public class MainActivity extends Activity {? MapView mMapView = null;? ? BaiduMap bmap = null;? Boolean traffic= false;??? LocationClient lclient;? Boolean first_post = true;? Context thiscontext =null;???? @Override? ???? protected void onCreate(Bundle savedInstanceState) {? ???????? super.onCreate(savedInstanceState);?? ???????? requestWindowFeature(Window.FEATURE_NO_TITLE);???????? SDKInitializer.initialize(getApplicationContext());? ???????? setContentView(R.layout.activity_main);? ???????? ???????? thiscontext=this;???????? mMapView = (MapView) findViewById(R.id.bmapView);? ???????? MapStatusUpdate mfactory = MapStatusUpdateFactory.zoomTo(15.0f);???????? bmap=mMapView.getMap();???????? bmap.setMapStatus(mfactory);???????? ???????? LocationMothod();???? }? ???? ???? @Override??protected void onStart() {???super.onStart();???bmap.setMyLocationEnabled(true);???if(!lclient.isStarted())???lclient.start();??}??@Override??protected void onStop() {???super.onStop();???bmap.setMyLocationEnabled(false);???lclient.stop();??}??@Override? ???? protected void onDestroy() {? ???????? super.onDestroy();? ???????? //在activity執(zhí)行onDestroy時執(zhí)行mMapView.onDestroy(),實現(xiàn)地圖生命周期管理? ???????? mMapView.onDestroy();? ???? }? ???? @Override? ???? protected void onResume() {? ???????? super.onResume();?????????? mMapView.onResume();? ???????? }? ???? @Override? ???? protected void onPause() {? ???????? super.onPause();?????????? mMapView.onPause();? ???????? }??@Override??public boolean onCreateOptionsMenu(Menu menu) {???getMenuInflater().inflate(R.menu.main, menu);???return super.onCreateOptionsMenu(menu);??}????public void LocationMothod(){???lclient=new LocationClient(this);???lclient.registerLocationListener(new MyListener());???LocationClientOption lcOption=new LocationClientOption();???lcOption.setCoorType("bd09ll");???lcOption.setIsNeedAddress(true);???lcOption.setOpenGps(true);???lcOption.setScanSpan(1000);???lclient.setLocOption(lcOption);??}????@Override??public boolean onOptionsItemSelected(MenuItem item) {??????switch(item.getItemId()){???case R.id.pmap:????bmap.setMapType(BaiduMap.MAP_TYPE_NORMAL);????break;???case R.id.wmap:????bmap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);????break;???case R.id.tmap:????if(!traffic){//如果是關(guān)閉狀態(tài)?????bmap.setTrafficEnabled(true);?????traffic=true;????}else{//如果是打開狀態(tài)?????bmap.setTrafficEnabled(false);?????traffic=false;????}????break;???}???return super.onOptionsItemSelected(item);??}? ???? ?class MyListener implements BDLocationListener{??@Override??public void onReceiveLocation(BDLocation arg0) {??????MyLocationData mlData = new MyLocationData.Builder()?????????????????????????? .accuracy(arg0.getRadius())?????????????????????????? .latitude(arg0.getLatitude())?????????????????????????? .longitude(arg0.getLongitude())?????????????????????????? .build();???bmap.setMyLocationData(mlData);???if(first_post){????LatLng llng=new LatLng(arg0.getLatitude(), arg0.getLongitude());????MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(llng);????bmap.animateMapStatus(msu);????//first_post=false;????Toast.makeText(thiscontext, arg0.getLatitude()+"---"+arg0.getLongitude(), Toast.LENGTH_SHORT).show();???}??}?}}不能定位自己的坐標地圖,顯示空白中間有一個位置原點,Toast打印出:4.9E-324---4.9E-324。
- 4 回答
- 1 關(guān)注
- 3128 瀏覽
添加回答
舉報
0/150
提交
取消