我目前修改了“谷歌地图视图”代码(如下所示),但是当地图加载并且您点击地图上的项目时,它会返回错误。
我认为这与mContext的null值有关,但我不确定,如果有人能帮我解决这个问题,我会非常感激:
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
我的地图课程:
package testing.map;
import java.util.List;
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;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class Mapview extends MapActivity {
/** Called when the activity is first created. */
protected boolean isRouteDisplayed(){
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.jd_sports_logo);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
//Declared longitude/latitude as doubles as GeoPoint only uses 'int' - converted to microdegrees
double latitude = 51.545538;
double longitude = -0.477247;
GeoPoint point = new GeoPoint((int)(latitude * 1e6), (int)(longitude * 1e6));
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
我的HelloItemizedOverlay类:
package testing.map;
import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import android.app.AlertDialog;
import android.content.Context;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context mContext = context;
}
@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();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
答案 0 :(得分:5)
您在主类中缺少上下文。
添加:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mContext);
where mContext = this;
而不是:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
答案 1 :(得分:1)
我还发现当您点按标记时,HelloGoogleMaps
教程代码失败了。
如果您传递this
(来自扩展MapActivity
的实例),则会显示对话框。
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
需要构造函数:
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
答案 2 :(得分:1)
更改两个地方:
1
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
2
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
答案 3 :(得分:0)
你可以传递这样的上下文,这里是伪代码:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(this,drawable);