What I want ?
When user taps on marker its image should change and user should be able to move the marker and I should be able to get the latitude and longitude . Below is a video of requirement .
What I have tried so far ?
I tried to implement default marker drag listener but it only works with long press .
I looked on similar questions on stack overflow but no luck .
Tried with ground overlay but not had much luck .
So how can I achieve this behaviour , Any hint is appreciated . There is already an app out there which does it so this should be possible .
As per documentation you are required to use:
GoogleMap.OnMarkerClickListener
public static interface GoogleMap.OnMarkerClickListener
Which Defines signatures for methods that are called when a marker is clicked or tapped.
With public method:
abstract boolean onMarkerClick(Marker marker)
Called when a marker has been clicked or tapped.
INSTEAD OF
GoogleMap.OnMapLongClickListener
public static interface GoogleMap.OnMapLongClickListener
with Public method:
public abstract void onMapLongClick (LatLng point)
Called when the user makes a long-press gesture on the map, but only if none of the overlays of the map handled the gesture. Implementations of this method are always invoked on the Android UI thread.
Related
I have an Activity with a fragment, this fragment has a map in it and some markers.
I run a service in the background that changes the position of the markers.
Each Marker has a boolean "isDrawn" and whenever the boolean is set to false the map is updated by adding drawing the marker ( I use LiveData to observe the markers)
Whenever I close the Activity that contains the fragment I call onDestroyView, which sets isDrawn of every marker to false. That way when I open the Activity again, the markers get drawn one more time. All of this works fine.
The problem is this: in the Fragment I can tap on the markers, which opens a view that has a button which opens another Activity, when I close this Activity (With BackButton) and if a marker changed its position (through the service) when I was in said Activity, I find that there are two of the same marker on the map.
Any idea what I could do? Should I remove the fragment and create it again when I close the activity? is that possible? if so how should I proceed?
Here you can try 2 options
Try creating a static variable and and store some flag values.
check whether the Fragment is visible with the help of isVisible() or getUserVisibleHint()
You must be using this method to add a marker to the map.
public final Marker addMarker (MarkerOptions options);
take one variable of type Marker and store the reference of it which is returned by addMarker (MarkerOptions options);
like
if(marker!=null){
marker.remove(); //will clear the previous one, it might be null at first.
}
marker=addMarker(yourOptions);//will add the latest one
I have opened "Google Map" Application from my App.
What I want is I want to add my app button for navigating to my app, over only google map not on other apps.
Using this link ,I have added overlayservice to my app but it will overdraw over every other application on device window.
I only want to draw over google map app Like this.
This started off as a comment, and then morphed into something of an answer. One option would be to draw a custom marker on your Google Map for the taxi icon. Whenever there is a zoom, drag, etc., you might have to do a re-render to get it in the same place again. I can verify that it is possible to capture click events on a custom marker in Android. Here is what your setup might look like:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMarkerClickListener(this);
mMap.setOnCameraMoveListener(this);
// ...
}
Then, override the marker click listener:
#Override
public void onCameraMove() {
// remove the old marker, and redraw it again on the left of the screen
}
You also probably would want to capture click events on your custom marker:
#Override
public boolean onMarkerClick(final Marker marker) {
// ...
}
The above onMarkerClick() method receives a reference to the marker which was clicked. You may keep an activity-scoped marker, and then check if it matches what was clicked to decide whether or not your custom marker were clicked.
I have a map with markers that sit pretty close to each other sometimes. When tapping one the click event also occurs on the markers that sit behind the one that was clicked. The listener is implemented as follows.:
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
presenter.onMarkerClicked(activeMarkers.get(marker.getId())
.getModelId());
return true;
}
});
Is there a way to avoid this without introducing extra state to my fragment?
you'll have to make tags for your markers so you can identify them... each marker has a setTag ..utilize that so you can differentiate them in the listener.
I'm using the setMyLocationEnabled(true) from Google Maps Android API v2 to have auto location tracking and to display the indicator just like Google Maps app. I need that the camera position follows the my-location indicator so it will be always visible, except when the user pans the map.
So, I'm using the onMyLocationChange method to move the camera to the new location, but when the user pans the map or use the search field from my app, the onMyLocationChange continues being called and the camera always comes back to the my-location coordinates.
How can I move the camera automatically when my-location is enabled, disable it when the map is manually manipulated (pan, rotate or search) and enable it back when the user press the my-location button?
Thanks.
Edit:
There is now GoogleMap.setOnMyLocationButtonClickListener, so you can skip point 1 and use that.
You can't do that with the february version of the Maps API v2. The best thing you can do is to:
have your own "go to my location" button to know when you want to start tracking
your own location change listener
a View on top of SupportMapFragment with onTouchListener (returning false!) to know when you need to stop tracking
This solution is not perfect, as it will stop tracking before user zooms on pans - it will stop when they first touch the map, but I think it is a solution that will not make any confusion for users: they touched the map, so can expect tracking to stop and can press the button to start it.
So Why don't you just why don't you just call setMyLocationEnabled(false) When user interacts with the TextView?
The only interaction you can check with the map is:
map.setOnMapClickListener(listener);
map.setOnMapLongClickListener(listener);
map.setOnMarkerClickListener(listener);
map.setOnMarkerDragListener(listener);
You could do the same thing for those interaction to disable the my-location layer.
Remove these two lines(if you have) from within the method
#Override
public void onLocationChanged(Location location) {
//Remove these two lines
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
You can detect a user gesture by using the REASON_GESTURE from the setOnCameraMoveStartedListener.
mMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
#Override
public void onCameraMoveStarted(int reason) {
if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
//Camera motion initiated in response to user gestures on the map.
} else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_API_ANIMATION){
//Non-gesture animation initiated in response to user actions.
} else if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_DEVELOPER_ANIMATION){
//Developer initiated animation.
}
}
});
See the doc for more details: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraMoveStartedListener
I'm creating an application with new Google Maps API V2 and I have to intercept the click on InfoWindow, showed when a Marker is clicked.
Reading the documentation I assumed that I do that to listen to InfoWindows clicks :
mGoogleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Log.d("", marker.getTitle());
}
});
But unfortunatly the method is never called.
If I try to listen to marker click and use setOnMarkerClickListener instead of OnInfoWindowClickListener , this works fine.
Hope to find some help, thanks in advance
The OnInfoWindowClickListener gets called when you actually click on the Marker title popup and not the marker.
The above just works fine.