我在使用Android开发者网站上的Hello Map Views教程时遇到了问题:http://developer.android.com/resources/tutorials/views/hello-mapview.html
我的第一个问题是,点击叠加项后,应用程序会崩溃。通过确保将上下文传递给我创建的ItemizedOverlay类来解决此问题...
解决此问题后,叠加图标不会显示在地图中。我可以单击叠加层所在的地图并显示对话框。不幸的是,我看不到图标。我确保我引用的图像是位于R.java资源文件中的对象。事实上,在经历相同的问题后,我的确切问题是由以下线程的海报提出的。不幸的是他的第二个问题从未得到解Context null Pointer
这是我的MapView类:
package com.mapsExample;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class HelloMaps extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
我的ItemizedOverlay课程:
package com.mapsExample;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@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;
}
}
同样,如果我没有将'this'传递给ItemizedOverlay构造函数,则会显示图标但无法单击。任何帮助深表感谢。提前谢谢!
答案 0 :(得分:10)
将第二个构造函数中的超级调用更改为:
超级(boundCenterBottom(defaultMarker));
应该有用!
答案 1 :(得分:0)
使用此功能查看您的图标:
MapController mapController = mapView.getController();
GeoPoint point = new GeoPoint(19240000,-99120000);
mapController.animateTo(g);