安卓java百度地图api文档_Android调用百度地图API 实时定位代码
[java]代碼庫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);
}
// 返回
總結
以上是生活随笔為你收集整理的安卓java百度地图api文档_Android调用百度地图API 实时定位代码的全部內容,希望文章能夠幫你解決所遇到的問題。