Android OverlayItem doesn't show a message - android

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

Related

Android MapView Overlay Image not displaying

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

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.

How to pass data from class to activity after confirming dialog? (drawing on mapview)

I have an extended overlay class:
short code:
public class MapOverlay extends Overlay {
private Context context;
private ProgressDialog dDialog;
Drawable drawable;
GeoPoint MainPoint;
MapView mapView;
public MapOverlay(Context context, MapView mapView)
{
this.context = context;
this.mapView = mapView;
}
#Override
public boolean onTap(GeoPoint p, MapView mapView)
{
this.MainPoint = p;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
setPoint();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog.show();
return true;
}
public void setPoint()
{
OverlayItem overlayitem = new OverlayItem(MainPoint, "Hi!", "You touched this location!");
}
I want to draw the touched point on my mapview, which is in this activity:
public class MyMapLocationActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
drawable = this.getResources().getDrawable(R.drawable.androidmarker);
MapOverlay myOverlay = new MapOverlay(this, mapView);
mapView.getOverlays().add(myOverlay);
mapView.postInvalidate();
}
I want to mark the touched point, after confirming the dialog box from MapOverlay class. I think I'm missing to pass something - what more should I do?
You need to have a class which extends ItemizedOverlay, which in turn can hold an OverlayItem, which takes a GeoPoint in its constructor. Something like
public class MapDemo extends MapActivity implements OnTouchListener {
private Drawable mDrawable;
private ItemizedMapOverlay mItemizedOverlay;
private OverlayItem mOverlayitem;
private GeoPoint mClickedPoint = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GeoPoint mapCentrePoint = new GeoPoint(51500000, 0);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setOnTouchListener(this);
MapController mapCtrlr = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapCtrlr.setZoom(8);
mapCtrlr.setCenter(mapCentrePoint);
List<Overlay> mapOverlays = mapView.getOverlays();
mDrawable = this.getResources().getDrawable(R.drawable.icon);
mItemizedOverlay = new ItemizedMapOverlay(mDrawable, this);
mapOverlays.add(mItemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {return false;}
#Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
final View fv = v;
AlertDialog.Builder dialog = new AlertDialog.Builder(v
.getRootView().getContext());
dialog.setMessage("Do you want to set point here?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
setPoint(fv);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
dialog.show();
// Can't show point till +ve button selected, so store it
mClickedPoint = ((MapView) v).getProjection()
.fromPixels((int)e.getX(), (int)e.getY());
}
return true;
}
void setPoint(View v) {
if (mClickedPoint != null) {
mOverlayitem = new OverlayItem(mClickedPoint, "test", "test2");
mOverlayitem.setMarker(mDrawable);
mItemizedOverlay.clear(); // clear last marker
mItemizedOverlay.addOverlay(mOverlayitem);
v.postInvalidate();
}
}
}
and
public class ItemizedMapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public ItemizedMapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public ItemizedMapOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
}
#Override
protected OverlayItem createItem(int i) {return mOverlays.get(i);}
#Override
public void draw(android.graphics.Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
public int size() { return mOverlays.size();}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
}
}
should be close enough for you to adapt.

Changing Map Infowindow from alert dialogue to simple map Info

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.

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

Categories

Resources