微信(WeChat)是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序,由张小龙所带领的腾讯广州研发中心产品团队打造 。微信支持跨通信运营商、跨操作系统平台通过网络快速发送免费(需消耗少量网络流量)语音短信、视频、图片和文字,同时,也可以使用通过共享流媒体内容的资料和基于位置的社交插件“摇一摇”、“漂流瓶”、“朋友圈”、”公众平台“、”语音记事本“等服务插件。
以了解一下。
最近项目需求把发送定位模块改成类似微信发送位置给好友的效果,我使用了高德地图实现了一个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;
}
4处的监听定位成功后会回调onLocationChanged这个方法,在这个方法里面我们可以获得定位到的经纬读,地址,显示出上面我们设置的自定义系统定位小蓝点出来等等,
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
//这个方法会循环执行
mLongitude = aMapLocation.getLongitude();//经度
mLatitude = aMapLocation.getLatitude();//纬度
cityCode = aMapLocation.getCityCode();//citycode
}
我们再来分析2处地图位置改变时回调:
@Override
public void onCameraChange(CameraPosition cameraPosition) {
}
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
/**这个方法很重要,虽然在上述的onLocationChanged方法我们也能获得坐标点的信息但是我们
通常在这里获得定位坐标的经纬度,获得了经纬度后我们并不能知道该坐标点的具体坐标信息,所以
需要把对应的坐标点转化为具体地址,这就是所谓的同步逆地理编码请求,和定位是黄金搭档
*/
mCurrentPoint = new LatLonPoint(cameraPosition.target.
latitude, cameraPosition.target.longitude);
// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
RegeocodeQuery query = new RegeocodeQuery(mCurrentPoint, 200, GeocodeSearch.AMAP);
mGeocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
}
3处我们做的地理正反编码回调如下:
//----------------逆地址编码回调:坐标->地址
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null &&
result.getRegeocodeAddress().getFormatAddress() != null) {
/**
* 汽车服务
软件推荐:
(网视站,专业提供浏览器下载)