Android MapView Overlay Image not displaying - android

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

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.

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.

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

OnTap() event on map is not fired

Hello i am unable to fire ontap() event
i want to add a marker whenever i tap on map, and when i tap on another geopoint the first marker should disappear and the marker should be added on new location...
till now i have come to this point..can anybody tell me where am i going wrong!!
thanks in advance
Source Code
public class GetLocation extends MapActivity implements OnClickListener {
MapView mapView;
MapController mc;
GeoPoint p;
int range;
String category;
Button view, traffic;
private static final String Tag = "GetLocation class";
ZoomControls zoomControls;
Canvas canvas;
MapOverlay itemizedoverlay;
List<Overlay> mapOverlays;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Tag","Inside onCreate");
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.myMapView1);
LinearLayout zoom = (LinearLayout)findViewById(R.id.zoom);
mapView.setReticleDrawMode(
MapView.ReticleDrawMode.DRAW_RETICLE_UNDER);
// Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
view=(Button)findViewById(R.id.BtnView);
traffic=(Button)findViewById(R.id.BtnTraffic);
Bundle extra=getIntent().getExtras();
if(extra != null)
{
category=extra.getString("category");
range=extra.getInt("range");
}
view.setOnClickListener(this);
traffic.setOnClickListener(this);
zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mc.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mc.zoomOut();
}
});
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
itemizedoverlay = new MapOverlay(drawable,this);
mc = mapView.getController();
mapView.invalidate();
mc.setZoom(17);
mapView.invalidate();
// mapView.setSatellite(true);
mapView.setStreetView(true);
// mapView.setOnClickListener(this);
Log.d("Tag","Exit onCreate");
}
class MapOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlay = new ArrayList<OverlayItem>();
private Context mContext;
private boolean isPinch = false;
public MapOverlay(Drawable defaultMarker,Context context) {
super(boundCenterBottom((defaultMarker)));
mContext = context;
// TODO Auto-generated constructor stub
}
public void addOverlayItem(OverlayItem overlayItem)
{
if(!mOverlay.contains(overlayItem)){
mOverlay.add(overlayItem);
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return null;
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlay.size();
}
public boolean onTap(GeoPoint p, MapView map)
{
if ( isPinch )
{
Log.i("onTap","in if!");
return false;
}
else
{
Log.i("onTap","TAP!");
if ( p!=null )
{
OverlayItem overlayitem = new OverlayItem(p," ", " ");
itemizedoverlay.addOverlayItem(overlayitem);
mapOverlays.add(itemizedoverlay);
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + ",on Tap" +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
Log.d("Tag","Exit TAp");
return true; // We handled the tap
}
else
{
return false; // Null GeoPoint
}
}
}
}
this is my code
hope you can get some idea
Point p1=new Point(0,0);
mapView.getProjection().toPixels(mapPoint, p1);// mapPoint is GeoPoint object
inDrag=item; // item get from List object and inDrag is an OverlayItem object
items.remove(inDrag); items is list object
populate();
GeoPoint pt=mapView.getProjection().fromPixels(p1.x+xDragImageOffset,p1.y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),inDrag.getSnippet());
items.add(toDrop);
populate();
Have a look to this article: http://mobiforge.com/developing/story/using-google-maps-android
It has a part called: "Adding Markers"
Also, here you have another more advanced example:
https://github.com/commonsguy/cw-advandroid/blob/master/Maps/NooYawkTouch/src/com/commonsware/android/maps/NooYawk.java
Hope it helps you.
the function on tap in itemizedoverlay is basically fired when the user taps on a marker
u wil hav to create a class extending overlay to detect taps .
check this overlays example

Why are my overlays on a mapview not shown?

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.

Categories

Resources