Replace marker with another one - android

IN my android map application I am trying to replace marker on touch event.This is my overlay item class and my default marker defined in main class .
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
Bitmap marker = BitmapFactory.decodeResource( null, R.drawable.image_pin);//Replace with this marker
private ArrayList<OverlayItem> myOverlaysNormal ;
Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlaysNormal = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlaysNormal.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlaysNormal.get(i);
}
#Override
protected boolean onTap(int index) {
myOverlaysNormal.get(index).getPoint();
return true;
}
// Removes overlay item i
public void removeItem(int i){
myOverlaysNormal.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlaysNormal.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlaysNormal.add(overlayItem);
populate();
}
}
How can I replace touched marker with another marker(R.Drawable.image_pin)..
OR How can I expand the marker default marker?

There is multi-way to do it i hope i can help in your code but u should make good search and good read.
here some link will help u :
First Example
2 example

Related

ItemizedOverlay OnTap() not firing

I've defined a map overlay, and I can display markers without issue. I'm now trying to get something to happen when I tap one, but the event never seems to fire. I'm sure I'm missing something obvious...
public class MapBlobCollection extends ItemizedOverlay<OverlayItem> {
#SuppressWarnings("serial")
public class ItemTappedEvent extends EventObject
{
public ItemTappedEvent(int itemIndex) {
super(itemIndex);
}
}
private ArrayList<OverlayItem> myOverlays ;
public MapBlobCollection(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
super.onTap(index);
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return true;
}
}
Basically, the debug log entry isn't written and the event doesn't fire.
In addition, my mapview itself doesn't pan around (should it, without any extra code from me?) and despite setting the setBuitInZoomControls(true), these don't appear either... so perhaps the mapview itself is at fault?
The mapview is defined in the layout as:
<com.google.android.maps.MapView
android:id="#+id/indexMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="#string/mapskey_release"/>
And I'm not overriding any draw events or anything...
I believe you need to add
android:clickable="true"
to your mapview
Try moving the super function to the end:
#Override
protected boolean onTap(int index) {
Log.d("TESTING","Triggering tap event on " + Integer.toString(index));
EventManager.triggerEvent(this, new ItemTappedEvent(index));
return super.onTap(index);
}

Programmatically adding and removing markers

I've been following the NooYawk MapView example with reasonable success. I've replaced the hardcoded geopoints and descriptions based on some system update messages. New markers are added just fine when a new update occurs. The problem is the markers don't go away when the problem is resolved.
I'd be happy enough to remove all markers on press of the refresh button as they get added back in.
Any ideas?
The below is somewhat sanitized.
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
try {
data = getData();
} catch (MalformedURLException e) {
//
}
if (!data.equals("")) {
// process data
for (Integer i = 0; i < outages.length; i++) {
items.add(new OverlayItem(
getPoint(lat, lng, headerMsg, bodyMsg));
}
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, false);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
on Refresh Button click mapview.getOverlays().clear(); Then start push pin in map and after adding Overlays you should mapview.postinvalidate()

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

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