在XMl中写一个button点击进行定位:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击定位" />
<com.baidu.mapapi.map.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent">
添加button的点击事件:
public void onClick(View view) {
mLocationClient = new LocationClient(this);
mBdLocationListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
latitude = bdLocation.getLatitude();
longitude = bdLocation.getLongitude();//通过地图定位服务获取当前位置
markerOptions.position(new LatLng(latitude, longitude));
map.addOverlay(markerOptions);
}
};
MapStatus mapStatus = new MapStatus.Builder()
.target(new LatLng(latitude,longitude))
.zoom(14).build();//zoom设置显示放大的级别,target设置显示位置
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
map.setMapStatus(mapStatusUpdate);
mLocationClient.registerLocationListener(mBdLocationListener);//此方法用于更新地图显示的位置
mLocationClient.start();
}