Opening InfoWindow automatically when adding marker Google Maps v2 Android
According to the documents of Google Maps for Android V2:
An info window allows you to display information to the user when they
tap on a marker on a map. By default, an info window is displayed when
a user taps on a marker if the marker has a title set. Only one info
window is displayed at a time. If a user clicks on another marker, the
current window will be hidden and the new info window will be
displayed. You can show an info window programmatically by calling
showInfoWindow() on the target marker. An info window can be hidden by
calling hideInfoWindow().
You can show the info window like this:
Marker marker = myMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Title")
.snippet("Snippet")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker)));
marker.showInfoWindow();
Android Google Maps snippet auto update in InfoWindow
I found solution using Android Maps Extensions https://github.com/mg6maciej/android-maps-extensions.
Call this after update markers data:
Marker m = map.getMarkerShowingInfoWindow();
if (m != null && !m.isCluster()) {
m.showInfoWindow();
}
Dynamic contents in Maps V2 InfoWindow
You should be doing Marker.showInfoWindow()
on marker that is currently showing info window when you receive model update.
So you need to do 3 things:
- create model and not put all the downloading into
InfoWindowAdapter
- save reference to Marker (call it
markerShowingInfoWindow
)
fromgetInfoContents(Marker marker)
- when model notifies you of download complete call
if (markerShowingInfoWindow != null && markerShowingInfoWindow.isInfoWindowShown()) {
markerShowingInfoWindow.showInfoWindow();
}
Google Maps Android v2 - Detecting Marker InfoWindow Close
You can only have a single InfoWindow
open at a time, which means you can track the one that's open:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
});
Now on each map click you can check if the task on a specific marker is still running:
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
if (mLastMarker != null) {
mLastMarker = null;
// Stop task
}
}
});
If you want, you can also stop the tasks when changing the InfoWindow
:
@Override
public View getInfoWindow(Marker marker) {
if (mLastMarker != null) {
// Stop task for mLastMarker
}
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}
Google Maps: Center Auto-Opening infoWindow
Based on this answer: https://stackoverflow.com/a/10722973/1023562
Create a function that will offset your center by some pixels:
function map_recenter(map,latlng,offsetx,offsety) {
var point1 = map.getProjection().fromLatLngToPoint(
(latlng instanceof google.maps.LatLng) ? latlng : map.getCenter()
);
var point2 = new google.maps.Point(
( (typeof(offsetx) == 'number' ? offsetx : 0) / Math.pow(2, map.getZoom()) ) || 0,
( (typeof(offsety) == 'number' ? offsety : 0) / Math.pow(2, map.getZoom()) ) || 0
);
map.setCenter(map.getProjection().fromPointToLatLng(new google.maps.Point(
point1.x - point2.x,
point1.y + point2.y
)));
}
Invoke this function using projection_changed
event (because map.getProjection()
is not available before this event ocurs):
google.maps.event.addListenerOnce(map,"projection_changed", function() {
map_recenter(map, occ, 0, -100);
});
Working fiddle: http://jsfiddle.net/kz1hnec0/4/
NOTE:
I have set Y offset to -100. If you know how high your info window will be (it's 155px in this case), then you can pass this value a bit more precise - I guess that would be something like (infoWindow.height + infoWindow.pixelOffset / 2)
.
Related Topics
Fragmentactivity Cannot Be Resolved to a Type
How to Get the Battery Level After Connect to the Ble Device
Android Device Is Not Connected to Usb for Debugging (Android Studio)
Hax Kernel Module Is Not Installed
Using an Android Library Project Activity Within Another Project
How to Return String or JSONobject from Asynchronous Callback Using Retrofit
Android:Load Svg File from Web and Show It on Image View
How to Move an Image from Left to Right in Android
How to Launch the 'Add Contact' Activity in Android
Android: Changing Nfc Settings (On/Off) Programmatically
Saving Arraylists in SQLite Databases
Opencv Image Comparison in Android
How to Align Textview Around an Imageview