I am creating a map application using GoogleMap. I have a problem while moving a CustomMarker from first to second location with help of location change listener. CustomMarker gets duplicated because its maintaining the older position as well while updating to new location. Please refer the below image.
Duplication of single custom marker
Why won't you just clear() GoogleMap in order to remove old marker?
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#clear()
Related
I am currently using 2 activities, the first will load the map and there will be a menu option called "add place" when this is clicked the user fills it in and then it is sent back to the main activity where it then adds a marker with this String. The problem is that I can only add a marker to a specific location where I define the latitude and longitude using GeoPoint. I want to be able to add the marker on the last location which the map was positioned. Could anyone give me any ideas as to how I could go about this?
You can get geopoint for current center of the map:
IGeoPoint point = mapView.getMapCenter()
And then use it for marker construction.
I am trying to use osmdroid to display a map with items. Looking at the API's of ItemizedOverlay (and derived) and OverlayItem I can not see a way to modify items (change position and marker) once they are created.
OverlayItem has functions to change the marker but not the position.
As my items are going to change position rather often I wonder if:
osmdroid has a better API hidden away somewhere
I have to replace the overlay every time one of its items position changes.
I can mess around with the list of Items replacing items
something else I haven't thought of.
PS: Digging some more I discovered that extending ItemizedOverlay would be the way. This way I can mess around with the items, but I still have to call populate() every time I change a coordinate and thus discard the internal list...
That's one reason why ItemizedOverlay and derived are hated, and the Marker has been added to osmdroid, as simple and flexible as its Google Maps API equivalent.
I'm making an application where the user can create a set of markers and then have the option to export them in some sort of file, so that not only can the map (with the markers) can be saved and reopened, other people using the app can import this file into their application.
At the moment, every time I create a marker, I add the marker object to an arrayList. Here is the code for that:
List<Marker> markers = new ArrayList<Marker>();
Marker m = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title(title)
.snippet(snippet)
.draggable(true));
markers.add(m);
This seems to work reasonably well; I am able to get the properties of each marker through methods such as getTitle() or getSnippet() etc.
However, everytime the activity is destroyed, this contents of this arrayList disappears as well, thus I feel as though I need to save this information is some file. Here are my questions:
Is saving objects in a text file a good idea? Or should I save strings and integers (only the info i need, rather than the whole marker) and use something else instead of textfiles (sharedprefences maybe?)?
How would I be able to do this? Examples for the export and import functions of all the markers would be really helpful.
Whenever I rotate the device, the activity is destroyed, thus all the info is gone. Will i have to export and import everytime this happens?
Any help or insight to any of these questions would help a lot :)
you can use content provider for sharing data between app.
see this https://developer.android.com/guide/topics/providers/content-provider-creating.html
for avoiding to refresh after screen rotation add this line to your manifest file
android:configChanges="keyboardHidden|orientation|screenSize"
setLocationEnabled is a attribute that shows a button on the top of the map that makes the map go to the current place.
But I want to "hide" this Button and call the method on the another custom button. Is there a way? I don't wanna to create a big code to go to current location, it's not fast. I just wanna a button with the same function of the setLocationEnabled(true).
How do I do that?
I've dealt with a similar problem and I found a pretty detailed solution from here Google map for android my location custom button
Have fun!
As you may know, you can hide the current location button with the following:
UiSettings.setMyLocationButtonEnabled(false);
Now, if you want to perform this task manually, you'll have to do some work on your own. First, you will need to acquire the user's current location. This can be acquired (and updated) by overriding the onLocationChanged function of the LocationListener implementation. If you are unclear on how to do this, checkout the documentation describing it. This may prove useful as well!
Secondly, you'll need to target the user's location by setting the map's camera to look at it, and (possibly) set a marker in the location. Here's the function I used in one of my recent apps describing how to do so:
public void moveToLocation(Location input) {
Double lat = (double) (input.getLatitude());
Double lon = (double) (input.getLongitude());
final LatLng location = new LatLng(lat, lon);
setLocMarker(location);
mainMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16));
//if you want the camera to have an "animated" effect, you can perform the following
//mainMap.animateCamera(CameraUpdateFactory.zoomTo(16), 4000, null);
}
For more information regarding camera effects and animations, refer to this documentation.
I am building an Android application that shows the user his/her current location (using MyLocationOverlay), and I am also using a LocationManager/LocationListener to get the user's location every 15 seconds, which is then used to query a web service and in return I plot points using an ItemizedOverlay.
This is all working with no problems.
Should I be using BOTH MyLocationOverlay as well as LocationManager/LocationListener, however? It seems redundant. What would you do? From what I can see, MyLocationOverlay uses the LocationManager in the background to constantly receive location updates, anyway.
Thanks!
The MyLocationOverlay does a little more such as adding the overlay to the map, showing the error radius and making it cool and blinky! There is nothing wrong with using it in conjunction with getting your own updates. In my app I decided not to use it because I wanted to change the way that it looked so that it was more like what you see in the Maps app where the MyLocationOverlay is an arrow that shows what direction you're facing. It's really a matter of preference.