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()
Related
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
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);
}
So I have a mapView and I want to dynamically populate it based on a database. I can do each geopoint indidivually but for some reason when i try to do them all it doesn't display the points on the map.. If anyone has suggestions please let me know :-)
public class FieldTrip extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
private String theLat;
private String theLong;
private Double theDLat;
private Double theDLong;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maptastic);
map=(MapView)findViewById(R.id.mapView);
map.getController().setCenter(getPoint(40.76793169992044,
-73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.supaaaa);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
#Override
public void onResume() {
super.onResume();
me.enableCompass();
}
#Override
public void onPause() {
super.onPause();
me.disableCompass();
}
#Override
protected boolean isRouteDisplayed() {
return(false);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
///need to make this dy-NAMIK :p
public SitesOverlay(Drawable marker) {
super(marker);
for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
{
// get the FieldTripStop object from the current hash table entry
Statics.currentFTStop = cur.getValue();
// concatenate numbers before (all) and after (6) the decimal, since
// geopoints only accept 6 numbers past the decimal.
theLat = Statics.currentFTStop.latitude;
theLong = Statics.currentFTStop.longitude;
theDLat = Double.parseDouble(theLat);
theDLong = Double.parseDouble(theLong);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(theDLat,
theDLong),
"RANDOM TEXT?",
"RANDOM TEXT LALALA"));
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
protected boolean onTap(int i) {
Toast.makeText(FieldTrip.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
}
This code looks correct to me. If you are not seeing all of the points, then fieldTripStops doesn't have all of the points you wish to display. Double check the elements in the fieldTripStops set.
On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i update the location i end up with multiple markers on the map instead of just one, the current location. is there any way to remove the previous marker before adding the next one?
I followed the steps in this tutorial : http://developer.android.com/resources/tutorials/views/hello-mapview.html
And my code goes like this :
public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** 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);
Log.i("AppTwo", "making connectionhandler object");
conhandler = new ConnectionHandler();
conhandler.execute();
Log.i("AppTwo", "making button");
this.refreshButton = (Button)this.findViewById(R.id.close);
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("AppTwo", "inside onclick");
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
conhandler.write();
lat = conhandler.retLat();
lon = conhandler.retLon();
lat = lat * 1e6;
lon = lon * 1e6;
int ilat = (int) lat;
int ilon = (int) lon;
GeoPoint point = new GeoPoint(ilat ,ilon);
OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
, Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and my AppTwoAndroidItemizedOverlay class is :
public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
#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;
}
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
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();
}
}
In the onClick method just give
mapOverlays.clear();
this should clear any existing markers.
Hope this works :)
In this part of code:
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
When your removing the Overlay from the mapOverlay structure you are not really clearing the overlay, so when you add other item and re-add it to the mapOverlay there will be 2 markers.
If you just want a single marker do an Overlay that has setOverlayItem instead of a list with an 'adding' logic. (meaning do a overlay with just an item that when you add another, just replaces the old one)
Hope it helped! :D
Add this statement before the code....
mMap.clear();
mMap.clear();
LatLng latLng = new LatLng(gps.getLatitude(), gps.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(gps.getLatitude(), gps.getLongitude()), 15));
mMap.addMarker(new MarkerOptions().position(latLng).title(""));
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
I have the following code and the markers are not appearing on the map at all!
private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
private List<pfOverlayItem> items=new ArrayList<pfOverlayItem>();
//private PopupPanel panel=new PopupPanel(R.layout.popup);
public SitesOverlay() {
super(null);
items = mainOverlayArray;
populate();
}
#Override
protected pfOverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
public int size() {
return(items.size());
}
private Drawable getMarker(int resource) {
Drawable marker=getResources().getDrawable(resource);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
boundCenter(marker);
return(marker);
}
}
mainOverlayArray is full of pfOverlayItem's and the code for that class is
public class pfOverlayItem extends OverlayItem {
private String coolText;
public String getcoolText() {
return coolText;
}
public void setcoolText(String coolText) {
this.coolText = coolText;
}
public pfOverlayItem(GeoPoint point, String title, String snippet) {
super(point, title, snippet);
// TODO Auto-generated constructor stub
}
}
I also set the marker outside of this after processing an XML file...
ArrayList<pfOverlayItem> overArray = myXMLHandler.getOverlayArray();
mainOverlayArray = overArray;
pfOverlayItem tempOver = null;
Drawable marker = getResources().getDrawable(R.drawable.icon);
for (int i = 0; i < mainOverlayArray.size(); i++) {
tempOver = mainOverlayArray.get(i);
tempOver.setMarker(marker);
}
sites=new SitesOverlay();
myMapView.getOverlays().add(sites);
myMapView.invalidate(); [/code]
It looks as though you're starting from one of my many sample Google Map applications. Your code as shown here is incomplete (e.g., according to the code here, you never create any OverlayItem instances).
My recommendation is you roll back to one of the samples I link to above and start modifying from there, or you start trying to figure out which of your methods are getting called and which are not.