Changing Map Infowindow from alert dialogue to simple map Info - android

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.

Related

Android: Making drawables clickable

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.

onTap on map-marker give me NullPointerException

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());

Android OverlayItem doesn't show a message

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

How to make an overlapped image clickable in android?

I have a image of a map and i want to plot some points/markers on the map. These markers must be clickable. I have used a custom view class to plot the markers at my required points.The markers are getting plotted but the problem is that either the marker is not clickable or when i set onClick listener on the custom view,the whole image receives the click event(whereever i click on image, onClick is called,but i want only click on the marker to initiate onClick). Can someone please help me with this?
You have to create your custom MapOverlay and inside put the code the function. In this code above, Override the OnTap method.
public class MapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MapOverlay(Drawable defaultMarker,Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clearOverlay() {
mOverlays.clear();
populate();
}
#Override
public int size() {
// TODO Auto-generated method stub
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;
}
}

OnClick event on canvas.drawCircle Android

I want to display few circles in google maps on my android application.
I want that when user clicks these circle it should show a toast based on the circle clicked.
I am using code.google.android.maps.overlay to display circle on a specific lat/long.
I am unable to find a solution.
Extend the ItemizedOverlay class
public class MapItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Drawable myPic;
private Activity mapActivity;
public MapItemizedOverlay(Drawable defaultMarker, Activity context) {
super(boundCenterBottom(defaultMarker));
this.mapActivity = context;
this.myPic = defaultMarker;
}
protected boolean onTap(int index) {
OverlayItem item = mOberlays.get(index);
... //Toast code
}
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();
}
}
this is a class which handles overlayitems.
There you can implement the onTap()-Method and show Toasts.
In your MapActivity you simply create this MapitemizedOverlay and add your items.
MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(circleDrawable, this);

Categories

Resources