Rectangle 27 49

You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

You could likely override this method and have it only open the marker and return true to consume the event.

// Since we are consuming the event this is necessary to
// manage closing opened markers before opening new ones
Marker lastOpened = null;

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) {
        // Check if there is an open info window
        if (lastOpened != null) {
            // Close the info window
            lastOpened.hideInfoWindow();

            // Is the marker the same marker that was already open
            if (lastOpened.equals(marker)) {
                // Nullify the lastOpened object
                lastOpened = null;
                // Return so that the info window isn't opened again
                return true;
            } 
        }

        // Open the info window for the marker
        marker.showInfoWindow();
        // Re-assign the last opened such that we can close it later
        lastOpened = marker;

        // Event was handled by our code do not launch default behaviour.
        return true;
    }
});

This is untested code but that may be a workable solution.

Yeah! Thanks! In my case i just call the onMapClick(marker.getPosition()) so that the click on the marker acts like a click on the map at the marker's position. Then, return true to notify that the event was handled.

@RuslanYanchyshyn Check this question. Maybe you can set up an onTouchEvent for the screen, use getProjection to get the map coordinates and ignore the marker click with a flag if the click wasn't within a set +/- of the parkers position. stackoverflow.com/questions/4177305/

Don't snap to marker after click in android map v2 - Stack Overflow

android google-maps android-mapview android-maps-v2
Rectangle 27 49

You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

You could likely override this method and have it only open the marker and return true to consume the event.

// Since we are consuming the event this is necessary to
// manage closing opened markers before opening new ones
Marker lastOpened = null;

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) {
        // Check if there is an open info window
        if (lastOpened != null) {
            // Close the info window
            lastOpened.hideInfoWindow();

            // Is the marker the same marker that was already open
            if (lastOpened.equals(marker)) {
                // Nullify the lastOpened object
                lastOpened = null;
                // Return so that the info window isn't opened again
                return true;
            } 
        }

        // Open the info window for the marker
        marker.showInfoWindow();
        // Re-assign the last opened such that we can close it later
        lastOpened = marker;

        // Event was handled by our code do not launch default behaviour.
        return true;
    }
});

This is untested code but that may be a workable solution.

Yeah! Thanks! In my case i just call the onMapClick(marker.getPosition()) so that the click on the marker acts like a click on the map at the marker's position. Then, return true to notify that the event was handled.

@RuslanYanchyshyn Check this question. Maybe you can set up an onTouchEvent for the screen, use getProjection to get the map coordinates and ignore the marker click with a flag if the click wasn't within a set +/- of the parkers position. stackoverflow.com/questions/4177305/

Don't snap to marker after click in android map v2 - Stack Overflow

android google-maps android-mapview android-maps-v2
Rectangle 27 1

It appears we should be able to do the following, but due to this bug, it doesn't work because marker.isInfoWindowShown() always returns false:

However, the following has the same effect and does work:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    Marker currentShown;

    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(currentShown)) {
            marker.hideInfoWindow();
            currentShown = null;
        } else {
            marker.showInfoWindow();
            currentShown = marker;
        }
        return true;
    }
});

Because only one info window is displayed at a time (as stated in the Google Maps API v2 Developer Guide), we only have to bother about hiding the info window if the marker whose info window is currently open is clicked.

Don't snap to marker after click in android map v2 - Stack Overflow

android google-maps android-mapview android-maps-v2
Rectangle 27 1

It appears we should be able to do the following, but due to this bug, it doesn't work because marker.isInfoWindowShown() always returns false:

However, the following has the same effect and does work:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    Marker currentShown;

    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(currentShown)) {
            marker.hideInfoWindow();
            currentShown = null;
        } else {
            marker.showInfoWindow();
            currentShown = marker;
        }
        return true;
    }
});

Because only one info window is displayed at a time (as stated in the Google Maps API v2 Developer Guide), we only have to bother about hiding the info window if the marker whose info window is currently open is clicked.

Don't snap to marker after click in android map v2 - Stack Overflow

android google-maps android-mapview android-maps-v2
Rectangle 27 52

Mymap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent intent = new Intent(MapActivity.this,OtherActivity.class);
               startActivity(intent);


            }
        });

How would you pass a parameter with a marker?

what is Mymap? Mine cannot resolve symbol 'setOnInfoWindowClickListener' because Mymap is not correct for my code... Is it the instance of the GoogleMap class? Is it the Class for the Activity? Or is it a new separate class inheriting from GoogleMap class?

@AlexVPerl Hi it maybe odd, but I use the title with a json string. Then you need to use a custom infowindow adapter to correctly display your content. Something like {"title":"Your Title","param1":"yourParam}.

Android Google Map v2 - Starting activity when clicking on marker info...

android google-maps android-activity google-maps-markers infowindow
Rectangle 27 52

Mymap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent intent = new Intent(MapActivity.this,OtherActivity.class);
               startActivity(intent);


            }
        });

How would you pass a parameter with a marker?

what is Mymap? Mine cannot resolve symbol 'setOnInfoWindowClickListener' because Mymap is not correct for my code... Is it the instance of the GoogleMap class? Is it the Class for the Activity? Or is it a new separate class inheriting from GoogleMap class?

@AlexVPerl Hi it maybe odd, but I use the title with a json string. Then you need to use a custom infowindow adapter to correctly display your content. Something like {"title":"Your Title","param1":"yourParam}.

Android Google Map v2 - Starting activity when clicking on marker info...

android google-maps android-activity google-maps-markers infowindow
Rectangle 27 141

For the button group you have outlined in red, you can disable it using the setMapToolbarEnabled() method in UISettings.

Sets the preference for whether the Map Toolbar should be enabled or disabled. If enabled, users will see a bar with various context-dependent actions, including 'open this map in the Google Maps app' and 'find directions to the highlighted marker in the Google Maps app'.

//Disable Map Toolbar:
mMap.getUiSettings().setMapToolbarEnabled(false);

Just in case you were also wondering about the zoom buttons, you can disable them like this:

mMap.getUiSettings().setZoomControlsEnabled(false);

How to hide "Navigation" and "GPS Pointer" buttons when I click the ma...

android google-maps google-maps-android-api-2 marker
Rectangle 27 16

You cannot triger a marker click directly.

If you need to run default implementation when onMarkerClick returns false (or you have no OnMarkerClickListener), you need to do it yourself:

marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);

250 is assumed here, API doesn't give you the value used internally for this default marker click behaviour.

If you have a OnMarkerClickListener, you can just keep the reference and call a function on it sending marker as a param.

Hmm, do you mean to say that I will not be able to trigger a marker click through my code ? I understand how to use a marker click listener (I am actually using it my code), but I wanted to trigger the click through my code without having to touch/click on the marker.

I gave you a workaround. There is no such method Marker.doClick(), but you can call OnMarkerClickListener's method yourself without direct interaction with the library.

Btw. you may want to submit a feature request here: gmaps-api-issues or here: Android Maps Extensions (or both).

hi this works but i would like to show navigate buttons in onmarkerclick

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 16

You cannot triger a marker click directly.

If you need to run default implementation when onMarkerClick returns false (or you have no OnMarkerClickListener), you need to do it yourself:

marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);

250 is assumed here, API doesn't give you the value used internally for this default marker click behaviour.

If you have a OnMarkerClickListener, you can just keep the reference and call a function on it sending marker as a param.

Hmm, do you mean to say that I will not be able to trigger a marker click through my code ? I understand how to use a marker click listener (I am actually using it my code), but I wanted to trigger the click through my code without having to touch/click on the marker.

I gave you a workaround. There is no such method Marker.doClick(), but you can call OnMarkerClickListener's method yourself without direct interaction with the library.

Btw. you may want to submit a feature request here: gmaps-api-issues or here: Android Maps Extensions (or both).

hi this works but i would like to show navigate buttons in onmarkerclick

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 16

You cannot triger a marker click directly.

If you need to run default implementation when onMarkerClick returns false (or you have no OnMarkerClickListener), you need to do it yourself:

marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);

250 is assumed here, API doesn't give you the value used internally for this default marker click behaviour.

If you have a OnMarkerClickListener, you can just keep the reference and call a function on it sending marker as a param.

Hmm, do you mean to say that I will not be able to trigger a marker click through my code ? I understand how to use a marker click listener (I am actually using it my code), but I wanted to trigger the click through my code without having to touch/click on the marker.

I gave you a workaround. There is no such method Marker.doClick(), but you can call OnMarkerClickListener's method yourself without direct interaction with the library.

Btw. you may want to submit a feature request here: gmaps-api-issues or here: Android Maps Extensions (or both).

hi this works but i would like to show navigate buttons in onmarkerclick

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 5

Define one HashMap with Marker as key and Alert as value and when you create new Marker to add on map at time add this Marker as key in HashMap and respective Alert data from alertsList,get specific Alert data from HashMap when particular Marker info window clicked.

public class AlertViewOnMap extends Activity {

    //declarations
    ArrayList<String> dateCreatedAtList = new ArrayList<String>();

    TextView busNumberTextView;
    TextView descriptionTextView;
    TextView alertTimeTextView;

    DateFormat dateFormat = new SimpleDateFormat("HH:mm");
    private HashMap<Marker,Alert> markerDataMap;


    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(com.fourbox.bocterapp.R.layout.details_design);

        busNumberTextView = (TextView) findViewById(R.id.textViewAlertBusNumber);
        descriptionTextView = (TextView) findViewById(R.id.textViewAlertDescription);
        alertTimeTextView = (TextView) findViewById(R.id.textViewAlertTime);

        busNumber = getIntent().getIntExtra("busNumber", 0);
        description = getIntent().getStringExtra("description");
        coordinatesLatitude =  getIntent().getDoubleExtra("coordinatesLatitude", 0);
        coordinatesLongitude = getIntent().getDoubleExtra("coordinatesLongitude", 0);

        alertTime.setTime(getIntent().getLongExtra("createdAt", 0));

        busNumberList = getIntent().getStringArrayListExtra("busNumberList");
        descriptionList = getIntent().getStringArrayListExtra("descriptionList");
        coordinatesLatitudeList =     getIntent().getStringArrayListExtra("coordinatesLatitudeList");
        coordinatesLongitudeList =     getIntent().getStringArrayListExtra("coordinatesLongitudeList");
        dateCreatedAtList = getIntent().getStringArrayListExtra("dateCreatedAtList");

        GoogleMap mMap;
        mMap = ((MapFragment)     getFragmentManager().findFragmentById(com.fourbox.bocterapp.R.id.mapFragment)).getMap();
        reuniteAlertListFromGetExtra();
        placeAllMarkersOnMap(mMap, alertsList);

        LatLng latLng = new LatLng(coordinatesLatitude, coordinatesLongitude);

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLng) // Center Set
                .zoom(18.0f)                // Zoom
                .bearing(0)                // Orientation of the camera to east
                .tilt(30)                   // Tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        busNumberTextView.setText(String.valueOf(busNumber));
        descriptionTextView.setText(description);
        alertTimeTextView.setText(String.valueOf(dateFormat.format(alertTime)));
    }

    public void placeAllMarkersOnMap(GoogleMap mMap, ArrayList<Alert> alertsList) {
        markerDataMap = new HashMap<Marker, Alert>();
        for(int i=0; i<alertsList.size(); i++) {
            markerDataMap.put(mMap.addMarker(new MarkerOptions()
                    .position(new     LatLng(alertsList.get(i).getCoordinates().getLatitude(),     alertsList.get(i).getCoordinates().getLongitude()))
                    .title(alertsList.get(i).getDescription())
                    .snippet(String.valueOf(alertsList.get(i).getBusNumber())
                    )),alertsList.get(i));

        }
    }

    public void reuniteAlertListFromGetExtra() {

        for (int i =0; i<busNumberList.size(); i++) {

            Alert alert = new Alert();
            ParseGeoPoint parseGeoPoint = new ParseGeoPoint();

            parseGeoPoint.setLatitude(Double.valueOf(coordinatesLatitudeList.get(i)));
            parseGeoPoint.setLongitude(Double.valueOf(coordinatesLongitudeList.get(i)));

            alert.setBusNumber(Integer.valueOf(busNumberList.get(i)));
            alert.setDescription(descriptionList.get(i));
            alert.setCoordinates(parseGeoPoint);
            alert.setCreatedAt(new Date(Long.valueOf(dateCreatedAtList.get(i))));

            alertsList.add(alert);
        }
    }

    public GoogleMap.OnMarkerClickListener getInfoMarkerClickListener() {
        return new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Alert alert = markerDataMap.get(marker);
                busNumberTextView.setText(alert.getDescription());
                descriptionTextView.setText(alert.getBusNumber());
                alertTimeTextView.setText(dateCreatedAtList.get(alert.getId()));
                marker.showInfoWindow();
                return true;
            }
        };
    }
}

java - Making a marker carry custom info to be used when clicked (Andr...

java android google-maps google-maps-markers
Rectangle 27 18

The question is: How do I identify what entity the clicked Marker 'marker' represents? [...] There's also a marker.getId(), but such ID is generated by the API and I can't control it

You can control it. The marker is returned by addMarker(), so you can get its ID and store it. Here's the code:

Map <String, Integer> mMarkers = new HashMap<String, Integer>();
MarkerOptions mo = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker))
.position(new LatLng(mLat, mLon))
.flat(true) 
.snippet("Click here for details")
.rotation(0)
.title(title);

When you add the marker to the map, store its ID on the container

Marker mkr = map.addMarker(mo);
mMarkers.put(mkr.getId(), myObject.getId());

Then when the marker is clicked, you can recover the id of "myObject" like this

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    public void onInfoWindowClick(Marker marker) {
        int id = mMarkers.get(marker.getId());
        // Do stuff with the id                         
    }
});

java - How to know which Marker was clicked on Google Maps v2 for Andr...

java android google-maps google-maps-markers
Rectangle 27 13

int zoom = (int)map.getCameraPosition().zoom
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(new LatLng(arg0.getPosition().latitude + (double)90/Math.pow(2, zoom), arg0.getPosition().longitude), zoom);

So I ended up with this solution, it works like a charm. I`m opening infoWindow after animation.

This is great. I'm curious where the math comes from. Were these just appropriate values you came up with that would incorporate the zoom level, or was there theory involved in which these numbers are specific?

I knew that world width is 256dp * 2^zoomLevel. arg0.getPosition().latitude was my pin latitude. 90 was just a factor that i needed, and I got it by calculating distance i want to move camera at certain zoom level, let's say that at zoom 14 i needed to move camera by ~0,006 degree to have infoView visible. So based on this i calculated factor 90, and this is working:) My infoView was too big to fit in map if pin was centered, so i wanted to animate zoom to point above my pin. One thing... it is working for integer zoom levels, so i just cast it to integer.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

android - How to center map on marker click to show whole InfoWindow -...

android maps center marker
Rectangle 27 5

Marker marker;
 GoogleMap mMap;

 mMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latlng) {
            // TODO Auto-generated method stub

            if (marker != null) {
                marker.remove();
            }
            marker = mMap.addMarker(new MarkerOptions()
                    .position(latlng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            System.out.println(latlng);

       }
   });

This works, but the blue point that indicates my position remains at my position. and the new marker is not a blue dot, but a red water drop thingy upside down?

Never mind, I got rid of the blue dot :)

Android Google Map v2 - move marker to clicked position and update geo...

android google-maps
Rectangle 27 5

Marker marker;
 GoogleMap mMap;

 mMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latlng) {
            // TODO Auto-generated method stub

            if (marker != null) {
                marker.remove();
            }
            marker = mMap.addMarker(new MarkerOptions()
                    .position(latlng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            System.out.println(latlng);

       }
   });

This works, but the blue point that indicates my position remains at my position. and the new marker is not a blue dot, but a red water drop thingy upside down?

Never mind, I got rid of the blue dot :)

Android Google Map v2 - move marker to clicked position and update geo...

android google-maps
Rectangle 27 2

OnMapClickListener is an Interface you can use to detect clicks on Google Maps V2. Kind of a "trigger" like in js. You set a Listener for specific events and you wait until you got some. Then you declare a method and you handle the event.

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 2

OnMapClickListener is an Interface you can use to detect clicks on Google Maps V2. Kind of a "trigger" like in js. You set a Listener for specific events and you wait until you got some. Then you declare a method and you handle the event.

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 2

OnMapClickListener is an Interface you can use to detect clicks on Google Maps V2. Kind of a "trigger" like in js. You set a Listener for specific events and you wait until you got some. Then you declare a method and you handle the event.

Triggering a map event (like a marker click) in Google maps Android V2...

android google-maps google-maps-markers google-maps-android-api-2
Rectangle 27 23

  • Return 'TRUE' to prevent GoogleMap by default moves the map center to the marker.
map.setOnMarkerClickListener(
    new OnMarkerClickListener() {
        boolean doNotMoveCameraToCenterMarker = true;
        public boolean onMarkerClick(Marker marker) {
            //Do whatever you need to do here ....
            return doNotMoveCameraToCenterMarker;
        }
    });

How to disable android map marker click auto center - Stack Overflow

android android-fragments google-maps-api-2
Rectangle 27 3

You are not obligated to show title when you have it set, so you can use that and as long as you return a View from getInfoContents and not setText on any subview of that returned View with title value.

List<Marker> policeMarkers
List<Marker> badGuysMarkers
if (policeMarkers.contains(marker)) { ... } else { ... }
Map<Marker, YourMarkerRelatedDataModel> allMarkers
YourMarkerRelatedDataModel model = allMarkers.get(marker);
Marker.setData(Object)
Object Marker.getData()
Map<Marker, Model>

java - How to know which Marker was clicked on Google Maps v2 for Andr...

java android google-maps google-maps-markers