❶ android百度地图api开发地图显示问题
试下下面的代码:
java">packagecom.liufeng.map;
importandroid.graphics.drawable.Drawable;
importandroid.os.Bundle;
importcom..mapapi.BMapManager;
importcom..mapapi.GeoPoint;
importcom..mapapi.MapActivity;
importcom..mapapi.MapController;
importcom..mapapi.MapView;
publicclassMainActivity蠢拿迹extendsMapActivity{
privateBMapManagermapManager;
privateMapViewmapView;
;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化MapActivity
mapManager=newBMapManager(getApplication());
//init方法的第一个参数需填入申请的APIKey
mapManager.init("",null);
super.initMapActivity(mapManager);
mapView=(MapView)findViewById(R.id.map_View);
//设置地图模式为交通地图
mapView.setTraffic(true);
//设置启用内置的缩放控件
mapView.setBuiltInZoomControls(true);
//用给定的经纬度构造一个GeoPoint(纬度,经度)
GeoPointpoint=newGeoPoint((int)(47.118440*1E6),(int)(87.493147*1E6));
//创建标记maker
Drawablemarker=this.getResources().getDrawable(R.drawable.iconmarka);
//为maker定义位置和带并边界
marker.setBounds(0,敏简0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
//取得地图控制器对象,用于控制MapView
mapController=mapView.getController();
//设置地图的中心
mapController.setCenter(point);
//设置地图默认的缩放级别
mapController.setZoom(12);
}
@Override
(){
returnfalse;
}
@Override
protectedvoidonDestroy(){
if(mapManager!=null){
mapManager.destroy();
mapManager=null;
}
super.onDestroy();
}
@Override
protectedvoidonPause(){
if(mapManager!=null){
mapManager.stop();
}
super.onPause();
}
@Override
protectedvoidonResume(){
if(mapManager!=null){
mapManager.start();
}
super.onResume();
}
}
❷ Android开发中百度地图的定位为什么总是北京,不是自己的位置
给,对照一下
可能的问题:1.用虚拟机,虚拟机定位就是在北京。
2.手机(或虚拟机)是否开启了GPS功能
------------------------------
public class MainActivity extends AppCompatActivity {
MapView mMapView = null;
private BaiMap mBaiDuMap;
public LocationClient mLocationClient;
// public BDLocationListener myListener = new MyLocationListener();
public MyLocationListener myListener = new MyLocationListener();
private BitmapDescriptor mCurrentMarker;
private MyLocationConfiguration.LocationMode mCurrentMode;
// 经纬度
private TextView tvJingDu;
// 纬度
private TextView tvWeiDu;
// 高度
private TextView tvGaoDu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
// 经纬度TextView 初始化
tvJingDu = (TextView) findViewById(R.id.tv_jing_);
tvWeiDu = (TextView) findViewById(R.id.tv_wei_);
tvGaoDu = (TextView) findViewById(R.id.tv_gao_);
//===================================
// 普通模式
mCurrentMode = MyLocationConfiguration.LocationMode.COMPASS;
// TODO
// mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.dingwei);
mCurrentMarker = null;
// 找到地图控件
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiDuMap = mMapView.getMap();// 获取地图
mBaiDuMap.setMapType(BaiMap.MAP_TYPE_NORMAL);// 设置地图模式为普通模式
// 开启定位图层
mBaiDuMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this); // 定位用到一个类
mLocationClient.registerLocationListener(myListener);// 注册监听
// TODO
mBaiDuMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker, R.color.myBlue, Color.YELLOW
));
// LocationClientOption类用来设置SDK的位置方式
LocationClientOption option = new LocationClientOption();//以下是给定位设置参数
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(2000);
mLocationClient.setLocOption(option);
mLocationClient.start();
}
boolean isFirstLoc = true; // 是否首次定位
// 3.网络位置监听
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
Log.i("监听被执行了", "监听被执行了");
if (location == null || mMapView == null) {
return;
}
// // 定位结果
// StringBuffer sb = new StringBuffer(256);
// StringBuffer sb1 = new StringBuffer(256);
// StringBuffer sb2 = new StringBuffer(256);
// // 经度
// String jd = sb.append(location.getLongitude()).toString();
// tvJingDu.setText("" + jd);
// Log.i("经度", "" + jd);
// // 纬度
// String wd = sb1.append(location.getLatitude()).toString();
// tvWeiDu.setText("" + sb1);
// Log.i("纬度", "" + wd);
//
// String gao = sb.append(location.getAltitude()).toString();
// tvGaoDu.setText("" + gao);
// 经度
double d1 = location.getLongitude();
String s1 = "" + d1;
tvWeiDu.setText(s1);
Log.i("经度", "" + s1);
// 纬度
double d2 = location.getLatitude();
Log.i("TGA", "d2" + d2);
String s2 = "" + d2;
tvJingDu.setText(s2);
Log.i("纬度", "" + s2);
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(0).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiDuMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
// 缩放的等级 12.0f
builder.target(ll).zoom(18.0f);
mBaiDuMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
Log.i("MyLocationListener被执行了!", "onReceiveLocation");
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
}
}
// 结束方法
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
}