Why getOverlays() is undefined for the MapView?

不打扰是莪最后的温柔 提交于 2019-12-14 03:07:28

问题


My qestion might be easy sinse I'm android begginer.

I'm following the Tutorial

and have succeed in a first part of creating a map activity. But now I'm trying to add an overlay items. The problem is in my .java code:

    List<Overlay> mapOverlays = mapView.getOverlays();

this line is presented in all tutorials. But I get the error message like: 'The method getOverlays() is undefined for the type MapView'. Can anybody help me to solve it?

code:

    package com.example.mapsdemo;
    import java.util.List;
    import com.google.android.gms.maps.MapView;
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.OverlayItem;
    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;

    public class MainActivity extends Activity {

private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
    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);

    }
}

The second class is here:

    public class HelloItemizedOverlay extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public Context mContext ;

public HelloItemizedOverlay(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
      super(boundCenterBottom(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;
}

}

Thanks for any help!


回答1:


You are mixing deprecated API v1 with API v2.

I'd suggest removing all the code and following this instead: https://developers.google.com/maps/documentation/android/start



来源:https://stackoverflow.com/questions/16518212/why-getoverlays-is-undefined-for-the-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!