package yu.hong.map; |
import com.baidu.mapapi.BMapManager; |
import com.baidu.mapapi.GeoPoint; |
import com.baidu.mapapi.LocationListener; |
import com.baidu.mapapi.MKGeneralListener; |
import com.baidu.mapapi.MKLocationManager; |
import com.baidu.mapapi.MapActivity; |
import com.baidu.mapapi.MapController; |
import com.baidu.mapapi.MapView; |
import com.baidu.mapapi.MyLocationOverlay; |
import android.location.Location; |
import android.os.Bundle; |
import android.view.KeyEvent; |
import android.view.Menu; |
import android.view.MenuItem; |
import android.widget.Toast; |
public class MainActivity extends MapActivity { |
// 初始为于勉庄 |
private double userLongitude = 33.49087222349736 * 1E6; // 纬度 |
private double userLatitude = 115.27130064453128 * 1E6; // 经度 |
// 添加百度相关控件 |
private MapView mapView; |
private BMapManager bMapManager; // 加载地图的引擎 |
// 百度地图上的key值 |
private String keyString = "01331AFA954E7E300428A5F0C9C829E0E16F87A3" ; |
// 在百度地图上添加一些控件,例如放大、缩小 |
private MapController mapController; |
private MKLocationManager mLocationManager; |
@Override |
public void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
// 实例化控件 |
mapView = (MapView) this .findViewById(R.id.bmapView); |
bMapManager = new BMapManager(MainActivity. this ); |
// 必须要加载key |
bMapManager.init(keyString, new MKGeneralListener() { |
// key值不正确 |
@Override |
public void onGetPermissionState( int arg0) { |
if (arg0 == 300 ) { |
Toast.makeText(MainActivity. this , R.string.key_error, |
Toast.LENGTH_LONG).show(); |
} |
} |
//网络出错 |
@Override |
public void onGetNetworkState( int arg0) { |
Toast.makeText(MainActivity. this , R.string.net_error, |
Toast.LENGTH_LONG).show(); |
} |
}); |
this .initMapActivity(bMapManager); |
mapView.setBuiltInZoomControls( true ); // 表示可以设置缩放功能 |
mapController = mapView.getController(); |
// 初始化Location模块 |
mLocationManager = bMapManager.getLocationManager(); |
// 通过enableProvider和disableProvider方法,选择定位的Provider |
mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER); |
mLocationManager.disableProvider(MKLocationManager.MK_GPS_PROVIDER); |
//返回手机位置 |
mLocationManager.requestLocationUpdates(locationListener); |
mLocationManager.setNotifyInternal( 5 , 2 ); |
// 添加定位图层 |
MyLocationOverlay myLocationOverlay = new MyLocationOverlay( this , |
mapView); |
myLocationOverlay.enableMyLocation(); // 启用定位 |
myLocationOverlay.enableCompass(); // 启用指南针 |
mapView.getOverlays().add(myLocationOverlay); |
mapView.setTraffic( true ); // 交通地图 |
// mapView.setSatellite(true);// 卫星地图 |
mapController.setZoom( 15 ); // 设置缩放级别 |
mapView.invalidate(); // 刷新地图 |
} |
//获取经度纬度 |
private LocationListener locationListener = new LocationListener() { |
@Override |
public void onLocationChanged(Location location) { |
if (location != null ) { |
userLatitude = location.getLatitude() * 1E6; |
userLongitude = location.getLongitude() * 1E6; |
GeoPoint mypoint = new GeoPoint(( int )(userLatitude), ( int )(userLongitude)); |
mapView.getController().animateTo(mypoint); |
} |
} |
}; |
// 销毁 |
@Override |
protected void onDestroy() { |
super .onDestroy(); |
if (bMapManager != null ) { |
bMapManager.destroy(); |
bMapManager = null ; |
} |
} |
// 停止 |
@Override |
protected void onPause() { |
super .onPause(); |
if (bMapManager != null ) { |
mLocationManager.removeUpdates(locationListener); |
bMapManager.stop(); |
} |
} |
// 重启 |
@Override |
protected void onResume() { |
super .onResume(); |
if (bMapManager != null ) { |
bMapManager.start(); |
} |
} |
// |
@Override |
protected boolean isRouteDisplayed() { |
return false ; |
} |
// Menu |
// 当点击Menu按钮时,调用该方法 |
@Override |
public boolean onCreateOptionsMenu(Menu menu) { |
menu.add( 0 , 1 , 1 , R.string.exit).setIcon( |
android.R.drawable.ic_menu_close_clear_cancel); |
return super .onCreateOptionsMenu(menu); |
} |
@Override |
public boolean onContextItemSelected(MenuItem item) { // 选中某个菜单项 |
if (item.getItemId() == 1 ) { |
MainActivity. this .finish(); |
} |
return super .onOptionsItemSelected(item); |
} |
// 返回 |