Having problem with onTap on markers in google map.
This is the error
05-31 21:46:21.420: E/AndroidRuntime(5541): java.lang.NullPointerException
05-31 21:46:21.420: E/AndroidRuntime(5541): at com.android.internal.app.AlertController$AlertParams.(AlertController.java:753)
05-31 21:46:21.420: E/AndroidRuntime(5541): at android.app.AlertDialog$Builder.(AlertDialog.java:273)
05-31 21:46:21.420: E/AndroidRuntime(5541): at my.class.HelloMapView$LocationOverlay.onTap(HelloMapView.java:1361)
And this is the code for the error at my.class.HelloMapView$LocationOverlay.onTap(HelloMapView.java:1361)
public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
//public class LocationOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> overLayList = new ArrayList<OverlayItem>();
private MapView mapView;
public String pickedlat;
public String pickedlng;
private Context mContext;
public LocationOverlay(MapView mapView, Drawable defaultMarker, Context context) {
//super(null);
super(boundCenterBottom(defaultMarker));
mContext = context;
this.mapView = mapView; // need it for onTap
populate();
}
#Override
protected OverlayItem createItem(int i) {
return overLayList.get(i);
}
#Override
public int size() {
return overLayList.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
if(!overLayList.contains(overlayItem)){
overLayList.add(overlayItem);
setLastFocusedIndex(-1);
populate();
}
// populate();
}
#Override
protected boolean onTap(int pIndex)
{
OverlayItem item = overLayList.get(pIndex);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
The line refered to in the errorlog is this
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
And what i can guess from my googling about it, it probably is the mContext not passed along... But i cant get it right...
Please help
Check your call to the constructor the null value might be passed in there. You can use this keyword since Activity is a subclass of Context like so
LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this);
Or if you are calling from a Fragment use the getActivity() method
LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this.getActivity());
Related
I'm trying to write a simple MapView using Google Maps API v1 (Targeting Gingerbread devices)
Just tried following the example hello-mapview and it all works apart from the image does not get displayed on the map.
There is an overlay, as the touch even works and displays the text, how ever no image appears with it.
Can anyone spot what I have missed?
MyMapView.java
public class MyMapView extends MapActivity {
private MapView mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = getResources().getDrawable(R.drawable.androidmarker);
NewOverlay itemizedoverlay = new NewOverlay(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() {
// TODO Auto-generated method stub
return false;
}
}
NewOverlay.java
public class NewOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public NewOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlays(){
mOverlays.clear();
populate();
}
#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;
}
}
Currently, I have an Android application that places icons on Google Maps. I was wondering if there was a way to make my drawables clickable so I can have a pop up menu giving the user some sort of menu options. I am implementing the drawables like this:
Drawable drawable1 = Drawable.createFromStream(getAssets().open("pics/pin1.png"), null);
CustomItemizedOverlay itemizedOverlay1 = new CustomItemizedOverlay(drawable1, this);
GeoPoint point1 = new GeoPoint(latitudep1, longitudep1);
OverlayItem overlayitem1 = new OverlayItem(point1, "Icon", "#1");
itemizedOverlay1.addOverlay(overlayitem1);
mapOverlays.add(itemizedOverlay1);
Thanks in advance!
try this way
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
Intent intent;
MapView mapview;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
/*intent = new Intent(context, Locationdisplay.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// changed
Bundle bundle = new Bundle();
bundle.putString("namevalue", item.getTitle());
intent.putExtras(bundle);
context.startActivity(intent);*/
Toast.makeText(context, "tapped", 1).show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
Not sure if possible, but can you display ImageViews instead of Drawables on your map? With ImageViews it would be easy to use the click events on them.
I have a map application in Android.
There is an alert dialogue when I click on marker, but I want to convert the alert dialogue to a simple infowindow like this:
http://3.bp.blogspot.com/-PrYuYQMcGcc/Ta__3fCcw7I/AAAAAAAAAGU/xCxjK3slU4k/s1600/google_map_marker_tooltip_popup_android.JPG
Here is the code I'm using to populate the Infowindow:
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
http://android-codes-examples.blogspot.in/2011/04/google-map-example-in-android-with-info.html
where did you get that image from??? its actually from this link and it also contains how to do it....but not onTap.
If you want onTap, just remove that alert dialog box and all from the onTap method and add a toast which tell the location...thats the general practice.
thats my code:
OverlayItem overlayItem = new OverlayItem(point, "Test", "Hello");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
When i run this app i can see the map with my item on it, but when click on it nothing happens. I expected a kind of messagebox with "Test [...] Hello" in it. I guess i forgot something. Thank you for help.
override the onTap(int index) as below...........
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
I followed the instructions from the google hellomapview tutorial. I get a working mapview etc. But the two items that are added to the map are not shown. It seems they are there somewhere because tapping at the specified location shows the message that was added to the items.
Edit
Here is my source code. It should be very close to the google tutorial source code.
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
private Context context;
public MapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
overlays = new ArrayList<OverlayItem>();
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
#Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
this.populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = overlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
public class MapsActivity 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);
MapOverlay overlay = new MapOverlay(this.getResources().getDrawable(
R.drawable.androidmarker), this);
overlay.addOverlay(new OverlayItem(new GeoPoint(19240000,-99120000), "Blubb", "See?"));
mapView.getOverlays().add(overlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Is the source code from the google tutorial available somewhere?
The problem is that I forgot to set the bounds of the drawable. It seems that if the mapview doesn't know how to align the image it won't show it at all.
I changed the first line in my constructor from:
super(defaultMarker);
to
super(boundCenterBottom(defaultMarker));
and know its working perfect.
At the same time, I have no idea how to help you directly.
Here are links to various editions of a project that definitely work with overlays, perhaps they will help.