views:

360

answers:

1

Hi there,

I'm building a google map application that marks several points on the map. I'm currently trying to get the dialog box to appear when the marker is clicked. Right now, the markers appear at the right places but when i click them, the entire application stops unexpectedly/forced to close. Does anyone have any suggestions on what is wrong and how i should go about fixing it? i would really appreciate any input!

thanks!!

*below is some of my code:

  • MyMapsActivity.java class:

package net.learn2develop.MyGoogleMapsNew;

import java.util.List;

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 MyMapsActivity extends MapActivity { /** Called when the activity is first created. */

public MyItemOverlay itemoverlay;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    //MapView = new MapView(this, "example_Maps_ApiKey_String");
    mapView.setBuiltInZoomControls(true);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    itemoverlay = new MyItemOverlay(drawable);

    GeoPoint point1 = new GeoPoint(19240000,-99120000);
    OverlayItem overlayitem1 = new OverlayItem(point1, "Hola, Mundo!", "I'm in Mexico City!");

    GeoPoint point2 = new GeoPoint(35410000, 139460000);
    OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");

    GeoPoint point3 = new GeoPoint(39000000, -77000000);
    OverlayItem overlayitem3 = new OverlayItem(point3, "","");


    itemoverlay.addOverlay(overlayitem1);
    itemoverlay.addOverlay(overlayitem2);
    itemoverlay.addOverlay(overlayitem3);

    mapOverlays.add(itemoverlay);

}

protected boolean isRouteDisplayed() {
    return false;
}

}


-MyItemOverlay class:

package net.learn2develop.MyGoogleMapsNew;

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 MyItemOverlay extends ItemizedOverlay {

private ArrayList mOverlays = new ArrayList(); private Context mContext;

public MyItemOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub }

public MyItemOverlay(Drawable defaultMarker, Context context) { super(defaultMarker); mContext = context; } @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; }

/* 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.setPositiveButton("Yes", new OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.show(); return true; } }*/

// @Override /protected OverlayItem createItem(int arg0) { // TODO Auto-generated method stub return null; }/

/@Override public int size() { // TODO Auto-generated method stub return 0; }/

@Override public int size() { return mOverlays.size(); }

public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); }

}


-AndroidManifest.xml file

</application>

<uses-permission 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="android.permission.INTERNET">
</uses-permission>

<uses-permission 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="android.permission.ACCESS_COARSE_LOCATION">
</uses-permission>
<uses-sdk android:minSdkVersion="3" />


Thank you very much!!!

A: 

change

itemoverlay = new MyItemOverlay(drawable);

to

itemoverlay = new MyItemOverlay(drawable, this);

in MyMapsActivity.java

Bobby Prabowo