Android: changing the location of an overlay - android

How do i go about changing the location of an overlay. Now, one way is to remove the original overlay n draw a new one. Doing this changes the index of all overlays drawn after it.
For my application, each overlay represent a user with a certain ID. when this user moves, i need to update his location. If i remove the overlay from the list, them the hashmap i am using to locate the overlay for this user, will be rendered useless.
How should I go about doing this?

There is no way that I know of. You can simply
// remove the old key association
hMap.remove(idForOverlay);
// add a new one
hMap.put(idForOverlay, newOverlayItem);
// add item to the overlays
mapView.getOverlays().add(newOverlayItem);
mapView.invalidate();
Actually you don't even have to remove() and put() will replace the current association with the newly added association with the key. The current association is returned as an Object

If I understand right you can change the overlay point and then call MapView.invalidate.

Related

Android google maps single tap on marker for drag & drop

I have an application where the user can drag & drop a pin on the map. But in order to drag and drop the pin you have to hold it for a few seconds, right?
Is there a way to drag the pin as soon as the user taps on it? Thanks
P.S using Google Maps for Android API v2
Drop the pin <==> Hide the pin??
You can do this, but you need to keep the reference of the pin that you have added in a variable. Do not simply add it, you need to store it to a variable then add this variable to the overlay. Later you can remove the required overlay, by simply calling remove() on that instance.

Giving different id to each marker "overlay" at google maps in Android

I want each marker on the map (Map View) with different id so that I can remove it or change its icon dynamically at run time.
How do we achieve that ?
Also please I want the best way to add many markers on the map without making it slow when moving it on taping over it.
Thanks in advance
How do we achieve that ?
Subclass OverlayItem and store whatever you want in it, using that instead of the ordinary OverlayItem class for your items. For example, this sample project has a CustomItem subclass of OverlayItem that, among other things, draws different icons at runtime.
Also please I want the best way to add many markers on the map without making it slow when moving it on taping over it
Don't "add many markers" to an ItemizedOverlay, to the point where it is "when moving it on taping over it". ItemizedOverlay is designed for small numbers of items.
If you need lots of items, you will most likely need to create your own Overlay, where you can have more smarts about which subset of your items will be relevant at any given time.

set marker to users location android-google-maps-api

I have a map application that follows the user and what I want to know is how to add a marker that follows the user. how close is it to just adding a marker at a set place? Can I switch the overlay to track somehow?
Thanks
mapView.getOverlays.clear() to remove the old overlay. And then place it again the way you placed the first overlay with the position you want. Place the overlay and the overlay item.

Android: Move point on the google map when clicking somewhere on the map

I want to give my users oppertinty to choose destination. from the beginning it will already have a point on the map, and after that they can choose/change to another location if they want..
how is it possible to change the point on the map, by clicking somewhere else on the map?
I assume you're talking about a MapView within your application, and not in the Google Maps app. There's a method on a MapView called onTouchEvent(). This callback will be called when a user clicks (or touches) on the map, and you can read the location of the touch from the MotionEvent object that's passed in. From there you can decide what you want to do.
Another way is to extend the Overlay class, and add it to your MapView's Overlay list. Overlay has a method called onTap(), which gives you the GeoPoint where the touch took place. You can then animate to that spot using the MapController to center the map on that spot.
If all you want to do is pan the map sideways, a user can simply touch and drag the map.
For these things to work the MapView must be clickable (settable in XML or with code).
The relevant reference pages are here:
URL to Android Maps API
i think this tutorial is perfect one. You should read carefully content of source code. That's all what u need to do.

How to save coordinates with Google Map?

I'm currently developing an app which uses tabs and google map. What I want to do is to get the gps positions, say 3, and store them in sql db (which I'm already doing) and then display them on the map. I already created canvas, added to overlay but those points disappear when I'm changing tabs so I thought if there is a way to somehow store those coords with google map so I can retrieve them and display them nicely whenever I'm clicking the "map tab"? Please can anyone help?
I don't have the answer for you but I know the issue is being caused by the change of view. This is the same as you get when you rotate the device and the screen resets. You need to do something with the onRestoreInstanceState(). The default of this restores the state of your UI. So you need to override it to save the data and readd it when the view changes.
Hope that helps. Someone else might have a code sample.
T
Use an ItemizedOverlay, and associate the overlay with the map in onCreate(). The overlay should not "disappear when I'm changing tabs".

Categories

Resources