基于Google的Android上地图应用问题,模拟器不显示,但真机能准确定位,Api Key没错(急)
只有2个Activity,第一个是欢迎界面,不说了,主Activity代码如下(图片是模拟器的显示效果):……publicclassMapsActivityextends...
只有2个Activity,第一个是欢迎界面,不说了,主Activity代码如下(图片是模拟器的显示效果):
……
public class MapsActivity extends MapActivity {
private MapView mapView;
private MapController mapController;
private GeoPoint p;
private String provider;
private LocationManager locationManager;
MyLocationOverlay mlo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* 初始化位置服务
* 初始化locationManager,并且找到最合适的LocationProvider
*/
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
final Location location=getCurrentLocation(locationManager);
/*----初始化地图显示和地图控制变量----*/
mapView=(MapView)findViewById(R.id.mapview);
mapController=mapView.getController();
mlo=new MyLocationOverlay(this, mapView);
mlo.enableMyLocation();
mlo.runOnFirstFix(new Runnable() {
@Override
public void run() {
mapController.animateTo(mlo.getMyLocation());
mapController.setZoom(16);
}
});
mapView.setClickable(true);
mapView.setEnabled(true);
mapView.setSatellite(false);
mapView.setTraffic(false);
mapView.setStreetView(false);
mapView.setBuiltInZoomControls(true); //设置缩放按钮可见
mapController.setZoom(16);
mapView.invalidate();
}
/**
* @return the current location
*/
private Location getCurrentLocation(LocationManager locationManager) {
Location location = locationManager.getLastKnownLocation("gps");
if (null != location) { return location; }
// getLastKnownLocation returns null if loc provider is not enabled
location = new Location("gps");
location.setLatitude(39);
location.setLongitude(116);
return location;
}
/*---------------添加Menu控件------------------*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.menu_help).setIcon(R.drawable.icon_help);
menu.add(0, 2, 2, R.string.menu_exit).setIcon(R.drawable.icon_exit);
return super.onCreateOptionsMenu(menu);
}
/*----------------Menu控件监听响应----------------------*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1: startActivity(new Intent(MapsActivity.this,aboutActivity.class));
break;
case 2: this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onDestroy() {
//locationManager.removeUpdates(locationListener);
super.onDestroy();
}
} 展开
……
public class MapsActivity extends MapActivity {
private MapView mapView;
private MapController mapController;
private GeoPoint p;
private String provider;
private LocationManager locationManager;
MyLocationOverlay mlo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* 初始化位置服务
* 初始化locationManager,并且找到最合适的LocationProvider
*/
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
final Location location=getCurrentLocation(locationManager);
/*----初始化地图显示和地图控制变量----*/
mapView=(MapView)findViewById(R.id.mapview);
mapController=mapView.getController();
mlo=new MyLocationOverlay(this, mapView);
mlo.enableMyLocation();
mlo.runOnFirstFix(new Runnable() {
@Override
public void run() {
mapController.animateTo(mlo.getMyLocation());
mapController.setZoom(16);
}
});
mapView.setClickable(true);
mapView.setEnabled(true);
mapView.setSatellite(false);
mapView.setTraffic(false);
mapView.setStreetView(false);
mapView.setBuiltInZoomControls(true); //设置缩放按钮可见
mapController.setZoom(16);
mapView.invalidate();
}
/**
* @return the current location
*/
private Location getCurrentLocation(LocationManager locationManager) {
Location location = locationManager.getLastKnownLocation("gps");
if (null != location) { return location; }
// getLastKnownLocation returns null if loc provider is not enabled
location = new Location("gps");
location.setLatitude(39);
location.setLongitude(116);
return location;
}
/*---------------添加Menu控件------------------*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.menu_help).setIcon(R.drawable.icon_help);
menu.add(0, 2, 2, R.string.menu_exit).setIcon(R.drawable.icon_exit);
return super.onCreateOptionsMenu(menu);
}
/*----------------Menu控件监听响应----------------------*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1: startActivity(new Intent(MapsActivity.this,aboutActivity.class));
break;
case 2: this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onDestroy() {
//locationManager.removeUpdates(locationListener);
super.onDestroy();
}
} 展开
展开全部
模拟器你是模拟设置了gps吗
更多追问追答
追问
怎么设置GPS呢?
追答
一、 (1)首先打开模拟器,然后运行cmd,输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不一样哈,具体端口号,模拟器左上方有显示的),这样会出现Android Console: type 'help' for a list of commands
OK的字样
(2)使用geo命令模拟发送GPS信号:geo fix 经度 纬度 比如:geo fix 116.337428 39.90923
(3)这时就会发现在模拟器的状态栏上多了一个GPS的标志~再使用
二、打开ddms-Emulator control - 里面有个经纬度设置 -然后send
如果上两种没效果的话 你把你的模拟器 edit 里hardware-new-gps support 选上 再试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询