| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package io.flutter.plugins.googlemaps; |
| |
| import com.google.android.gms.maps.GoogleMap; |
| import com.google.android.gms.maps.model.LatLng; |
| import com.google.android.gms.maps.model.Marker; |
| import com.google.android.gms.maps.model.MarkerOptions; |
| import io.flutter.plugin.common.MethodChannel; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| class MarkersController { |
| |
| private final Map<String, MarkerController> markerIdToController; |
| private final Map<String, String> googleMapsMarkerIdToDartMarkerId; |
| private final MethodChannel methodChannel; |
| private GoogleMap googleMap; |
| |
| MarkersController(MethodChannel methodChannel) { |
| this.markerIdToController = new HashMap<>(); |
| this.googleMapsMarkerIdToDartMarkerId = new HashMap<>(); |
| this.methodChannel = methodChannel; |
| } |
| |
| void setGoogleMap(GoogleMap googleMap) { |
| this.googleMap = googleMap; |
| } |
| |
| void addMarkers(List<Object> markersToAdd) { |
| if (markersToAdd != null) { |
| for (Object markerToAdd : markersToAdd) { |
| addMarker(markerToAdd); |
| } |
| } |
| } |
| |
| void changeMarkers(List<Object> markersToChange) { |
| if (markersToChange != null) { |
| for (Object markerToChange : markersToChange) { |
| changeMarker(markerToChange); |
| } |
| } |
| } |
| |
| void removeMarkers(List<Object> markerIdsToRemove) { |
| if (markerIdsToRemove == null) { |
| return; |
| } |
| for (Object rawMarkerId : markerIdsToRemove) { |
| if (rawMarkerId == null) { |
| continue; |
| } |
| String markerId = (String) rawMarkerId; |
| final MarkerController markerController = markerIdToController.remove(markerId); |
| if (markerController != null) { |
| markerController.remove(); |
| googleMapsMarkerIdToDartMarkerId.remove(markerController.getGoogleMapsMarkerId()); |
| } |
| } |
| } |
| |
| boolean onMarkerTap(String googleMarkerId) { |
| String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId); |
| if (markerId == null) { |
| return false; |
| } |
| methodChannel.invokeMethod("marker#onTap", Convert.markerIdToJson(markerId)); |
| MarkerController markerController = markerIdToController.get(markerId); |
| if (markerController != null) { |
| return markerController.consumeTapEvents(); |
| } |
| return false; |
| } |
| |
| void onMarkerDragEnd(String googleMarkerId, LatLng latLng) { |
| String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId); |
| if (markerId == null) { |
| return; |
| } |
| final Map<String, Object> data = new HashMap<>(); |
| data.put("markerId", markerId); |
| data.put("position", Convert.latLngToJson(latLng)); |
| methodChannel.invokeMethod("marker#onDragEnd", data); |
| } |
| |
| void onInfoWindowTap(String googleMarkerId) { |
| String markerId = googleMapsMarkerIdToDartMarkerId.get(googleMarkerId); |
| if (markerId == null) { |
| return; |
| } |
| methodChannel.invokeMethod("infoWindow#onTap", Convert.markerIdToJson(markerId)); |
| } |
| |
| private void addMarker(Object marker) { |
| if (marker == null) { |
| return; |
| } |
| MarkerBuilder markerBuilder = new MarkerBuilder(); |
| String markerId = Convert.interpretMarkerOptions(marker, markerBuilder); |
| MarkerOptions options = markerBuilder.build(); |
| addMarker(markerId, options, markerBuilder.consumeTapEvents()); |
| } |
| |
| private void addMarker(String markerId, MarkerOptions markerOptions, boolean consumeTapEvents) { |
| final Marker marker = googleMap.addMarker(markerOptions); |
| MarkerController controller = new MarkerController(marker, consumeTapEvents); |
| markerIdToController.put(markerId, controller); |
| googleMapsMarkerIdToDartMarkerId.put(marker.getId(), markerId); |
| } |
| |
| private void changeMarker(Object marker) { |
| if (marker == null) { |
| return; |
| } |
| String markerId = getMarkerId(marker); |
| MarkerController markerController = markerIdToController.get(markerId); |
| if (markerController != null) { |
| Convert.interpretMarkerOptions(marker, markerController); |
| } |
| } |
| |
| @SuppressWarnings("unchecked") |
| private static String getMarkerId(Object marker) { |
| Map<String, Object> markerMap = (Map<String, Object>) marker; |
| return (String) markerMap.get("markerId"); |
| } |
| } |