Why getOverlays() is undefined for the MapView?

2019-08-16 02:08发布

问题:

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