想做个功能,进入界面先定位自己的位置,然后在界面上可以输入一个中心点地点,以这个中心点地点画一个园,显示在地图上。代码如下:
public class Safety_MarginActivity extends Activity implements OnGetGeoCoderResultListener,OnClickListener {
private EditText city,center_et,range_et;
private Button range_confirm;
private MapView mMapView;
private BaiduMap mBaiduMap;
private LatLng centerPoint,currentPoint;
private GeoCoder mSearch;
private LocationClient mLocationClient;
private LocationMode mLocationMode;
private MyLocationListener mLocationListener;
private Marker mMarkerA, mMarkerB, mMarkerC, mMarkerD, mMarkerE;
private double mLatitude, mLongitude;
private float accuracy;
private boolean isFirstIn = true;
private String range;
private String location;
BitmapDescriptor iconMarkA = null;
BitmapDescriptor iconMarkB = null;
BitmapDescriptor mIconLocation;
private String username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.safety_margin);
//初始化界面
initView();
//进入界面后定位
initLocation();
//定位后添加覆盖物
initOverlay();
}
private void initView() {
mMapView = (MapView) findViewById(R.id.id_bmapView);
mBaiduMap = mMapView.getMap();
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
city = (EditText) findViewById(R.id.setting_safety_margin_center_city_et);
center_et = (EditText) findViewById(R.id.setting_safety_margin_center_et);
range_et = (EditText) findViewById(R.id.setting_safety_margin_range_et);
range_confirm = (Button) findViewById(R.id.range_confirm);
range_confirm.setOnClickListener(this);
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);
mBaiduMap.setMapStatus(msu);
mIconLocation = BitmapDescriptorFactory
.fromResource(R.drawable.navi_map_gps_locked);
iconMarkA = BitmapDescriptorFactory
.fromResource(R.drawable.icon_1);
iconMarkB = BitmapDescriptorFactory
.fromResource(R.drawable.icon_2);
}
private void initLocation() {
mLocationMode = LocationMode.NORMAL;
mLocationClient = new LocationClient(this);
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setCoorType("bd90ll");
option.setIsNeedAddress(true);
option.setScanSpan(3000);
mLocationClient.setLocOption(option);
}
private void initOverlay() {
LatLng lla = new LatLng(mLatitude, mLongitude);
ArrayList<BitmapDescriptor> gifList = new ArrayList<BitmapDescriptor>();
gifList.add(iconMarkA);
gifList.add(iconMarkB);
MarkerOptions moa = new MarkerOptions().position(lla).icons(gifList)
.zIndex(0).period(100);
mMarkerA = (Marker) mBaiduMap.addOverlay(moa);
}
@Override
protected void onStart() {
super.onStart();
mBaiduMap.setMyLocationEnabled(true);
mLocationClient.start();
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius()).direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
MyLocationConfiguration config = new MyLocationConfiguration(
mLocationMode, true, mIconLocation);
mBaiduMap.setMyLocationConfigeration(config);
if (isFirstIn) {
LatLng newll = new LatLng(location.getLatitude(), location.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(newll);
mBaiduMap.animateMapStatus(msu);
isFirstIn = false;
}
}
}
@Override
public void onGetGeoCodeResult(GeoCodeResult result) {
if(result==null||result.error!= SearchResult.ERRORNO.NO_ERROR){
Toast.makeText(Safety_MarginActivity.this, "抱歉,未能找到结果", Toast.LENGTH_SHORT).show();
return;
}
mBaiduMap.clear();
mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation()).icon(iconMarkA).zIndex(9));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
mLatitude = result.getLocation().latitude;
mLongitude =result.getLocation().longitude;
Toast.makeText(Safety_MarginActivity.this, mLatitude+","+mLongitude, Toast.LENGTH_SHORT).show();
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.range_confirm:
String center = center_et.getText().toString().trim();
String currentCity = city.getText().toString().trim();
range = range_et.getText().toString().trim();
mSearch.geocode(new GeoCodeOption().city(currentCity).address(center));
centerPoint = new LatLng(mLatitude, mLongitude);
Toast.makeText(Safety_MarginActivity.this, mLatitude+","+mLongitude+","+range, Toast.LENGTH_SHORT).show();
OverlayOptions safetyCircle = new CircleOptions().fillColor(0x000000FF).center(centerPoint).stroke(new Stroke(5, 0xAA000000))
.radius(Integer.parseInt(range));
mBaiduMap.addOverlay(safetyCircle);
break;
default:
break;
}
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
mMapView.onResume();
mLocationClient.stop();
super.onResume();
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
super.onDestroy();
mLocationClient.stop();
mMapView.onDestroy();
mSearch.destroy();
// 回收 bitmap 资源
iconMarkA.recycle();
iconMarkB.recycle();
mIconLocation.recycle();
}
}
进入界面后首先在地图上显示自己的位置就不对,离实际位置大概有1000米左右的距离,其次,选定好中心点画圆,园不显示。
能指点下,错误在哪里吗?
把initOverlay()
方法里面的换成这个是下:
LatLng lla = new LatLng(mLatitude, mLongitude);
OverlayOptions ooA = new MarkerOptions().position(lla).icon(bdA)
.draggable(false).extraInfo(bundle);
mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。