diff --git a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java index aebef7b9..a3ef606f 100644 --- a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java +++ b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java @@ -537,6 +537,48 @@ public void run() { } else { call.reject("map not found"); } + + } + }); + } + + @PluginMethod() + public void getViewBounds(final PluginCall call) { + final String mapId = call.getString("mapId"); + getBridge().executeOnMainThread(new Runnable() { + @Override + public void run() { + + CustomMapView customMapView = customMapViews.get(mapId); + + if (customMapView != null) { + JSObject result = new JSObject(); + JSObject bounds = new JSObject(); + JSObject farLeft = new JSObject(); + JSObject farRight = new JSObject(); + JSObject nearLeft = new JSObject(); + JSObject nearRight = new JSObject(); + + farLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.latitude); + farLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.longitude); + farRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.latitude); + farRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.longitude); + nearLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.latitude); + nearLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.longitude); + nearRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.latitude); + nearRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.longitude); + + bounds.put("farLeft",farLeft); + bounds.put("farRight",farRight); + bounds.put("nearLeft",nearLeft); + bounds.put("nearRight",nearRight); + result.put("bounds",bounds); + + call.resolve(result); + } else { + call.reject("map not found"); + } + } }); } diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index eb11aa41..6da93eed 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -26,4 +26,5 @@ CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback); + CAP_PLUGIN_METHOD(getViewBounds, CAPPluginReturnPromise); ) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index acc8eac1..bc92acd5 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -295,6 +295,47 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { setCallbackIdForEvent(call: call, eventName: CustomMapView.EVENT_DID_END_MOVING_CAMERA); } + @objc func getViewBounds(_ call: CAPPluginCall) { + let mapId: String = call.getString("mapId", "") + + DispatchQueue.main.async { + + guard let customMapView = self.customWebView?.customMapViews[mapId] else { + call.reject("map not found") + return + } + + let centerPoint = customMapView.GMapView.center + let centerCoords = customMapView.GMapView.projection.coordinate(for: centerPoint) + let bounds = customMapView.GMapView.projection.visibleRegion(); + + call.resolve([ + "bounds":[ + "farLeft": [ + "latitude": bounds.farLeft.latitude as Any, + "longitude": bounds.farLeft.longitude as Any + ], + "farRight":[ + "latitude": bounds.farRight.latitude as Any, + "longitude": bounds.farRight.longitude as Any + ], + "nearLeft":[ + "latitude": bounds.nearLeft.latitude as Any, + "longitude": bounds.nearLeft.longitude as Any + ], + "nearRight":[ + "latitude": bounds.nearRight.latitude as Any, + "longitude": bounds.nearRight.longitude as Any + ], + "center":[ + "latitude": centerCoords.latitude as Any, + "longitude": centerCoords.longitude as Any + ] + ] + ]) + } + } + func setCallbackIdForEvent(call: CAPPluginCall, eventName: String) { let mapId: String = call.getString("mapId", "") diff --git a/src/definitions.ts b/src/definitions.ts index ca79c1a9..aee3aa2a 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -16,6 +16,8 @@ import { AddMarkersOptions, AddMarkersResult, RemoveMarkerOptions, + ViewBoundsOptions, + ViewBoundsResult, // events DidTapInfoWindowCallback, DidCloseInfoWindowCallback, @@ -71,6 +73,8 @@ export interface CapacitorGoogleMapsPlugin { removeMarker(options: RemoveMarkerOptions): Promise; + getViewBounds(options: ViewBoundsOptions): Promise; + didTapInfoWindow( options: DefaultEventOptions, callback: DidTapInfoWindowCallback diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index ff01aba7..f019e94d 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -9,6 +9,7 @@ export { ElementFromPointResultOptions } from "./methods/ElementFromPointResult" export { AddMarkerOptions, AddMarkerResult } from "./methods/AddMarker"; export { AddMarkersOptions, MarkerInputEntry, AddMarkersResult } from "./methods/AddMarkers"; export { RemoveMarkerOptions } from "./methods/RemoveMarker"; +export { ViewBoundsOptions, ViewBoundsResult } from './methods/ViewBounds'; // events export * from "./events/DidTapInfoWindow"; diff --git a/src/interfaces/methods/ViewBounds.ts b/src/interfaces/methods/ViewBounds.ts new file mode 100644 index 00000000..7e11851c --- /dev/null +++ b/src/interfaces/methods/ViewBounds.ts @@ -0,0 +1,37 @@ +import { LatLng } from "./../../definitions"; +export interface ViewBoundsOptions { + /** + * The identifier of the map to which this method should be applied. + * + * @since 2.0.0 + */ + mapId: string; +} +export interface Bounds { + /** + * @since 2.0.0 + */ + farLeft: LatLng; + /** + * @since 2.0.0 + */ + farRight: LatLng; + /** + * @since 2.0.0 + */ + nearLeft: LatLng; + /** + * @since 2.0.0 + */ + nearRight: LatLng; + /** + * @since 2.0.0 + */ + center: LatLng; +} +export interface ViewBoundsResult { + /** + * @since 2.0.0 + */ + bounds: Bounds; +} \ No newline at end of file diff --git a/src/web.ts b/src/web.ts index 35f905f3..dabf77f3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -13,6 +13,8 @@ import { MoveCameraOptions, ElementFromPointResultOptions, AddMarkerOptions, + ViewBoundsOptions, + ViewBoundsResult, AddMarkerResult, AddMarkersOptions, AddMarkersResult, @@ -82,6 +84,10 @@ export class CapacitorGoogleMapsWeb throw this.unimplemented("Not implemented on web."); } + async getViewBounds(_options: ViewBoundsOptions): Promise { + throw new Error('Method not implemented on web.'); + } + async didTapInfoWindow( _options: DefaultEventOptions, _callback: DidTapInfoWindowCallback