我用谷歌地图开发应用程序。我按照Android开发者网站的材料和其他一些指导原则。我运行程序图标只指向位置。在背景中没有地图视图显示。对我有什么帮助吗?
提前致谢 问候 Lakshmanan。
这是我的源代码,
公共类MapPage扩展MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(1);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
java class :
public class HelloItemizedOverlay extends ItemizedOverlay
{
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
答案 0 :(得分:2)
我怀疑你没有指定正确的谷歌地图密钥。您需要指定为正在为您的应用程序签名的密钥库生成的Google地图密钥。
我没有看到代码中设置了密钥(如果要从xml布局构建MapView,则需要在MapView的构造函数中设置它或使用属性android:apiKey)。如果你不这样做,你通常会得到一个灰色的屏幕,覆盖层,然后在左下角有一个谷歌水印,我猜你得到了。 这是获取地图密钥的地方的链接。 Sign Up for the Android Maps Api
答案 1 :(得分:1)
我还认为您需要包含Maps API密钥。我更喜欢在单独的map.xml中包含:
<?xml version="1.0" encoding="utf-8"?>
<!-- The Api Key needs to be replaced corresponding to the signing certificate.
Check this site for more info:
http://code.google.com/intl/ko/android/maps-api-signup.html -->
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myMap"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:enabled="true" android:clickable="true"
android:apiKey="@+string/maps_api_key" />
然后我将此文件包含在我的任何地图视图中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myMapView" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/map" />
</RelativeLayout>