高德地图定位,点击标注
packagecom.amap.map2d.demo.basic;
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.location.Location;
importandroid.os.Bundle;
importcom.amap.api.location.AMapLocation;
importcom.amap.api.location.AMapLocationListener;
importcom.amap.api.location.LocationManagerProxy;
importcom.amap.api.location.LocationProviderProxy;
importcom.amap.api.maps2d.AMap;
importcom.amap.api.maps2d.AMap.OnMapClickListener;
importcom.amap.api.maps2d.CameraUpdateFactory;
importcom.amap.api.maps2d.LocationSource;
importcom.amap.api.maps2d.MapView;
importcom.amap.api.maps2d.model.BitmapDescriptorFactory;
importcom.amap.api.maps2d.model.LatLng;
importcom.amap.api.maps2d.model.Marker;
importcom.amap.api.maps2d.model.MarkerOptions;
importcom.amap.api.maps2d.model.MyLocationStyle;
importcom.amap.api.services.core.LatLonPoint;
importcom.amap.api.services.geocoder.GeocodeResult;
importcom.amap.api.services.geocoder.GeocodeSearch;
importcom.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
importcom.amap.api.services.geocoder.RegeocodeQuery;
importcom.amap.api.services.geocoder.RegeocodeResult;
importcom.amap.map2d.demo.R;
importcom.amap.map2d.demo.util.ToastUtil;
/**
*AMapV1地图中简单介绍OnMapClickListener,
*/
publicclassEventsActivityextendsActivityimplementsOnMapClickListener,LocationSource,
AMapLocationListener,OnGeocodeSearchListener{
privateAMapaMap;
privateMapViewmapView;
privateMarkermarker;
privateOnLocationChangedListenermListener;
privateLocationManagerProxymAMapLocationManager;
privateAMapLocationaMapLocation;
privateGeocodeSearchgeocoderSearch;
privateStringaddressName;
privateLatLonPointlatLonPoint;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.events_activity);
mapView=(MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);//此方法必须重写
init();
}
/**
*初始化AMap对象
*/
privatevoidinit(){
if(aMap==null){
geocoderSearch=newGeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
aMap=mapView.getMap();
setUpMap();
}
}
/**
*amap添加一些事件监听器
*/
privatevoidsetUpMap(){
aMap.setOnMapClickListener(this);//对amap添加单击地图事件监听器
//自定义系统定位小蓝点
MyLocationStylemyLocationStyle=newMyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory
.fromResource(R.drawable.location_marker));//设置小蓝点的图标
myLocationStyle.strokeColor(Color.BLACK);//设置圆形的边框颜色
myLocationStyle.radiusFillColor(Color.argb(100,0,0,180));//设置圆形的填充颜色
myLocationStyle.strokeWidth(1.0f);//设置圆形的边框粗细
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);//设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示
aMap.setMyLocationEnabled(true);//设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
}
/**
*方法必须重写
*/
@Override
protectedvoidonResume(){
super.onResume();
mapView.onResume();
}
/**
*方法必须重写
*/
@Override
protectedvoidonPause(){
super.onPause();
mapView.onPause();
deactivate();
}
/**
*方法必须重写
*/
@Override
protectedvoidonSaveInstanceState(BundleoutState){
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
*方法必须重写
*/
@Override
protectedvoidonDestroy(){
super.onDestroy();
mapView.onDestroy();
}
/**
*对单击地图事件回调
*/
@Override
publicvoidonMapClick(LatLngpoint){
if(marker!=null){
marker.remove();
}
//marker=aMap.addMarker(newMarkerOptions()
//.anchor(0.5f,0.5f).position(newLatLng(point.latitude,point.longitude))
////地图上的位置
//.title("aaaa")
////要显示的内容
//.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).draggable(false));
//marker.showInfoWindow();
latLonPoint=newLatLonPoint(point.latitude,point.longitude);
getAddress(latLonPoint);
}
/**
*此方法已经废弃
*/
@Override
publicvoidonLocationChanged(Locationlocation){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonProviderEnabled(Stringprovider){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonProviderDisabled(Stringprovider){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonGeocodeSearched(GeocodeResultresult,intrCode){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonRegeocodeSearched(RegeocodeResultresult,intrCode){
//TODOAuto-generatedmethodstub
if(rCode==0){
if(result!=null&&result.getRegeocodeAddress()!=null
&&result.getRegeocodeAddress().getFormatAddress()!=null){
addressName=result.getRegeocodeAddress().getFormatAddress()+"附近";
marker=aMap.addMarker(newMarkerOptions().anchor(0.5f,0.5f)
.position(newLatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude()))//地图上的位置
.title(addressName)//要显示的内容
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.draggable(false));
marker.showInfoWindow();
}else{
ToastUtil.show(EventsActivity.this,R.string.no_result);
}
}elseif(rCode==27){
ToastUtil.show(EventsActivity.this,R.string.error_network);
}elseif(rCode==32){
ToastUtil.show(EventsActivity.this,R.string.error_key);
}else{
ToastUtil.show(EventsActivity.this,
getString(R.string.error_other)+rCode);
}
}
/**
*响应逆地理编码
*/
publicvoidgetAddress(finalLatLonPointlatLonPoint){
RegeocodeQueryquery=newRegeocodeQuery(latLonPoint,200,
GeocodeSearch.AMAP);//第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
geocoderSearch.getFromLocationAsyn(query);//设置同步逆地理编码请求
}
/**
*定位成功后回调函数
*/
@Override
publicvoidonLocationChanged(AMapLocationaLocation){
//TODOAuto-generatedmethodstub
if(mListener!=null&&aLocation!=null){
mListener.onLocationChanged(aLocation);//显示系统小蓝点
if(aMapLocation==null){
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLatLng(aLocation.getLatitude(),aLocation.getLongitude()),16));
aMapLocation=aLocation;
latLonPoint=newLatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());
getAddress(latLonPoint);
}
}
}
/**
*激活定位
*/
@Override
publicvoidactivate(OnLocationChangedListenerlistener){
mListener=listener;
if(mAMapLocationManager==null){
mAMapLocationManager=LocationManagerProxy.getInstance(this);
/*
*mAMapLocManager.setGpsEnable(false);
*1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是trueLocation
*API定位采用GPS和网络混合定位方式
*,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
*/
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork,2000,10,this);
}
}
/**
*停止定位
*/
@Override
publicvoiddeactivate(){
mListener=null;
if(mAMapLocationManager!=null){
mAMapLocationManager.removeUpdates(this);
mAMapLocationManager.destory();
}
mAMapLocationManager=null;
}
}