以了解一下。
最近项目需求把发送定位模块改成类似微信发送位置给好友的效果,我使用了高德地图实现了一个demo,效果图如下:
从主界面中我们可以看到中心标记上面显示的就是我们定位的地址,下面是一个listview列表,第一条item的数据就是我们定位得到的地址,下面其余的都是我们根据定位得到的经纬度通过poi周边搜索得到的地址。我们进行了如下操作:
我们点击列表的item,中心标记会移动到该item对象的地址上面去。
我们手动移动地图的时候,中心标记的地址会发生相应的变化并且下面的列表也会发生相应的变化。
根据关键字poi搜索得到的列表,然后点击item主界面立马进行更新操作。
这里贴出主要代码,首先我们进行地图地位初始化操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
if (aMap==null) {
aMap=mapView.getMap();
// 自定义系统定位小蓝点
MyLocationStyle myLocationStyle=new MyLocationStyle();
// 设置小蓝点的图标
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
fromResource(R.mipmap.ic_location_marker));// 设置小蓝点的图标
myLocationStyle.strokeColor(0x7F0070D9);// 设置圆形的边框颜色
myLocationStyle.radiusFillColor(0x130070D9);// 设置圆形的填充颜色
// myLocationStyle.anchor(int,int)//设置小蓝点的锚点
myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(this);// 设置定位监听(1)
aMap.setonCameraChangeListener(this);//手动移动地图监听 (2)
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
//设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
aMap.setMyLocationEnabled(true);
aMap.moveCamera(CameraUpdateFactory.zoomTo(17.5f));
}
//------------------------------------------添加中心标记
mMarkerOptions=new MarkerOptions();
mMarkerOptions.draggable(false);//可拖放性
mMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_tips_nearby));
mCenterMarker=aMap.addMarker(mMarkerOptions);
ViewTreeObserver vto=mapView.getViewTreeObserver();
vto.addonGlobalLayoutListener(new ViewTreeObserver.onGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mapView.getViewTreeObserver().removeGlobalonLayoutListener(this);
mCenterMarker.setPositionByPixels(mapView.getWidth() >> 1, mapView.getHeight() >> 1);
mCenterMarker.showInfoWindow();
}
});
//---------------------------------------------初始化正反编码类 (3)
mGeocoderSearch=new GeocodeSearch(this);
mGeocoderSearch.setonGeocodeSearchListener(this);
}
我们注意重点关注在上面的三个监听回调,1处是定位监听,有以下两个回调方法:
//-----------------地图定位回调
//激活定位
@Override
public void activate(onLocationChangedListener onLocationChangedListener) {
mListener=onLocationChangedListener;
if (mlocationClient==null) {
mlocationClient=new AMapLocationClient(this);
mLocationOption=new AMapLocationClientOption();
//设置定位监听
mlocationClient.setLocationListener(this);(4)
//设置为高精度定位模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位参数
mlocationClient.setLocationOption(mLocationOption);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()
// 方法来取消定位请求
// 在定位结束后,在合适的生命周期调用onDestroy()方法
// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
mlocationClient.startLocation();
}
}
//停止定位
@Override
public void deactivate() {
mListener=null;
if (mlocationClient !=null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient=null;
}
- 下一篇: 普京指责乌克兰在俄罗斯村庄实施边境“恐怖主义行动”
- 上一篇: Suga将于5月1日参加《今夜秀》