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执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
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){//如果是关闭状态
bmap.setTrafficEnabled(true);
traffic=true;
}else{//如果是打开状态
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。
tzm1513
tzm1513
微笑了521
相关分类