views:

47

answers:

0

I am having trouble with the Hello Map Views tutorial from the Android Developer website: http://developer.android.com/resources/tutorials/views/hello-mapview.html

My first problem was that upon clicking the overlay item, the application would crash. This problem was solved by making sure to pass the context to the ItemizedOverlay class that I created...

After I fixed this problem, the icon for the overlay does not display in the map. I am able to click on the map where the overlay is located and the dialog box displays. Unfortunately, I cannot see the icon. I have made sure that the image that I reference is an object located in the R.java resources file. In fact my exact problem is asked by the poster of the following thread after going through the same issues. Unfortunately his second question was never answered. http://stackoverflow.com/questions/3531964/context-null-pointer

Here is my MapView class:

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;
    }
}

And my ItemizedOverlay class:

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;
 }

}

Again, If I do not pass 'this' to the ItemizedOverlay constructor, the icon displays but cannot be clicked. Any help is much appreciated. Thanks in advance!