您的位置:首页 > 移动开发 > Android开发

高德地图Android版SDK的应用(定位,添加自定义标记点)

2013-11-24 09:51 441 查看
需要先下载高德地图的 Android SDK和 Android 定位SDK : http://code.autonavi.com/index

package com.fproject.DXCBuy;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnInfoWindowClickListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;

import android.location.Location;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;

public class ShopMapActivity extends Activity implements LocationSource, AMapLocationListener, OnInfoWindowClickListener, InfoWindowAdapter, OnMarkerClickListener{
private PublicData pd;

private MapView mapView;
private AMap aMap;
private OnLocationChangedListener mListener;
private LocationManagerProxy mAMapLocationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopmap);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
init();

pd = PublicData.getInstance();
}

/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMap();
}
}

Marker MEIYIDUO;
Marker BOLIAN;
Marker CUNZHIHUA;
private void setUpMap() {
// 自定义系统定位小蓝点
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps));
myLocationStyle.strokeColor(Color.BLACK);
myLocationStyle.strokeWidth(5);
aMap.setMyLocationStyle(myLocationStyle);
// 设置定位资源
aMap.setLocationSource(this);
// 设置为true表示系统定位按钮显示并响应点击,false表示隐藏,默认是false
aMap.setMyLocationEnabled(true);

CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(23.041725,113.373472), 17);
aMap.moveCamera(update);

//添加标记
LatLng ll_meiyijia = new LatLng(23.041425,113.37536);
LatLng ll_bolian = new LatLng(23.041725,113.373472);
LatLng ll_cunzhihua = new LatLng(23.041337,113.372602);

MEIYIDUO = aMap.addMarker(new MarkerOptions()
.position(ll_meiyijia)
.title("美事多超市")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.cart_alt)).perspective(true)
.draggable(true));// 设置远小近大效果,2.1.0版本新增

BOLIAN = aMap.addMarker(new MarkerOptions()
.position(ll_bolian)
.title("博联超市")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.cart_alt)).perspective(true)
.draggable(true));// 设置远小近大效果,2.1.0版本新增

CUNZHIHUA = aMap.addMarker(new MarkerOptions()
.position(ll_cunzhihua)
.title("春之花超市")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.cart_alt)).perspective(true)
.draggable(true));// 设置远小近大效果,2.1.0版本新增

aMap.setOnMarkerClickListener(this);
aMap.setOnInfoWindowClickListener(this);
aMap.setInfoWindowAdapter(this);

BOLIAN.showInfoWindow();
}

/**
* 此方法需存在
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}

/**
* 此方法需存在
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
deactivate();
}

/**
* 此方法需存在
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

/**
* 此方法已经废弃
*/
@Override
public void onLocationChanged(Location location) {
}

/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null) {
// 将定位信息显示在地图上
mListener.onLocationChanged(aLocation);
}
}

/**
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy.getInstance(this);
/*
* mAMapLocManager.setGpsEnable(false);//
* 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true
*/
// Location SDK定位采用GPS和网络混合定位方式,时间最短是5000毫秒,否则无效
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 5000, 10, this);
}
}

/**
* 停止定位
*/
@Override
public void deactivate() {
mListener = null;
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(this);
mAMapLocationManager.destory();
}
mAMapLocationManager = null;
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public View getInfoContents(Marker marker) {
return null;
}

@Override
public View getInfoWindow(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.custom_info_window, null);

ImageView imageView = (ImageView) view.findViewById(R.id.badge);
imageView.setImageResource(R.drawable.cart_alt);

String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
SpannableString titleText = new SpannableString(title);
titleText.setSpan(new ForegroundColorSpan(Color.BLACK), 0,
titleText.length(), 0);
titleUi.setTextSize(15);
titleUi.setText(titleText);

TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
SpannableString snippetText = new SpannableString("点击进入商店");
snippetText.setSpan(new ForegroundColorSpan(Color.GRAY), 0,
snippetText.length(), 0);
snippetUi.setTextSize(12);
snippetUi.setText(snippetText);
return view;
}

@Override
public void onInfoWindowClick(Marker arg0) {
if(arg0.equals(MEIYIDUO)) {
pd.getHandlerMain().sendMsg("enter_shop", "myj");
} else if(arg0.equals(BOLIAN)) {
pd.getHandlerMain().sendMsg("enter_shop", "bl");
} else if(arg0.equals(CUNZHIHUA)) {
pd.getHandlerMain().sendMsg("enter_shop", "czh");
}
this.finish();
}

@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
return false;
}
}


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息