我有代码在地图上用标记显示两个位置,但它只显示标记,而不是地图,它只显示蓝屏。我在移动设备上运行,而不是模拟器。
我的代码:
public class HelloGoogleMaps3 extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView)findViewById(R.id.map);
map.getController().setCenter(getPoint
(40.76793169992044,-173.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return(false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
items.add(new OverlayItem(getPoint
(40.748963847316034,-173.96807193756104),
"UN", "United Nations"));
items.add(new OverlayItem(getPoint
(40.76866299974387,-173.98268461227417),
"Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint
(40.765136435316755,-173.97989511489868),
"Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint
(40.70686417491799,-174.01572942733765),
"The Downtown Club",
"Original home of the Heisman Trophy"));
populate();
}
@Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(HelloGoogleMaps3.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
@Override
public int size() {
return(items.size());
}
}
}
我做错了什么?
答案 0 :(得分:1)
这可能意味着以下两件事之一:
一个。您的API密钥不正确
湾您的坐标设置为(0,0),地图标记指向海洋。尝试缩放地图。你可能会看到这片土地。 :)
答案 1 :(得分:0)
如果您从位置管理器获取位置并尝试使用坐标而不先转换它们,就会发生这种情况:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// location.getLatitude() is, say, 56.8751841974
// location.getLongitude() is, say, -3.9548756201
GeoPoint mapPoint = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
基本上,您需要将值乘以100万以映射到正确的坐标。
答案 2 :(得分:0)
您可以使用不同的坐标集自定义MapView颜色,例如: