From 7aa39d8dbc7e78d7e3022cf812c413b5357e206a Mon Sep 17 00:00:00 2001 From: Jason Jozwiak Date: Sat, 7 May 2022 13:48:32 -0400 Subject: [PATCH 1/4] Adding viewBounds function to return map view bounds of all corners in Lat/Lng. Based on contribution by @J-Gonzalez PR #81 but updated to match project structure now being used in main branch --- .../CapacitorGoogleMaps.java | 42 +++++++++++++++++++ ios/Plugin/Plugin.m | 1 + ios/Plugin/Plugin.swift | 35 ++++++++++++++++ src/definitions.ts | 3 ++ src/interfaces/index.ts | 1 + src/interfaces/methods/ViewBounds.ts | 8 ++++ src/web.ts | 5 +++ 7 files changed, 95 insertions(+) create mode 100644 src/interfaces/methods/ViewBounds.ts diff --git a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java index aebef7b9..93c29f0f 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 viewBounds(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..50414be2 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(viewBounds, CAPPluginReturnPromise); ) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index acc8eac1..ea840e22 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -295,6 +295,41 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { setCallbackIdForEvent(call: call, eventName: CustomMapView.EVENT_DID_END_MOVING_CAMERA); } + @objc func viewBounds(_ 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 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 + ] + ] + ]) + } + } + func setCallbackIdForEvent(call: CAPPluginCall, eventName: String) { let mapId: String = call.getString("mapId", "") diff --git a/src/definitions.ts b/src/definitions.ts index ca79c1a9..c0e53694 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -16,6 +16,7 @@ import { AddMarkersOptions, AddMarkersResult, RemoveMarkerOptions, + ViewBoundsOptions, // events DidTapInfoWindowCallback, DidCloseInfoWindowCallback, @@ -71,6 +72,8 @@ export interface CapacitorGoogleMapsPlugin { removeMarker(options: RemoveMarkerOptions): Promise; + viewBounds(options: ViewBoundsOptions): Promise; + didTapInfoWindow( options: DefaultEventOptions, callback: DidTapInfoWindowCallback diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index ff01aba7..c7faa469 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 } 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..08085844 --- /dev/null +++ b/src/interfaces/methods/ViewBounds.ts @@ -0,0 +1,8 @@ +export interface ViewBoundsOptions { + /** + * The identifier of the map to which this method should be applied. + * + * @since 2.0.0 + */ + mapId: string; +} \ No newline at end of file diff --git a/src/web.ts b/src/web.ts index 35f905f3..186eb0e6 100644 --- a/src/web.ts +++ b/src/web.ts @@ -13,6 +13,7 @@ import { MoveCameraOptions, ElementFromPointResultOptions, AddMarkerOptions, + ViewBoundsOptions, AddMarkerResult, AddMarkersOptions, AddMarkersResult, @@ -82,6 +83,10 @@ export class CapacitorGoogleMapsWeb throw this.unimplemented("Not implemented on web."); } + async viewBounds(_options: ViewBoundsOptions): Promise { + throw new Error('Method not implemented on web.'); + } + async didTapInfoWindow( _options: DefaultEventOptions, _callback: DidTapInfoWindowCallback From 8595a58a46121e21f3a812eadc3b549a203c3888 Mon Sep 17 00:00:00 2001 From: Jason Jozwiak Date: Fri, 13 May 2022 11:46:19 -0400 Subject: [PATCH 2/4] Updates to viewBounds implementation Changing viewBounds to getViewBounds and adding ViewBoundsResult interface to provide a return type for the getViewBounds method --- .../CapacitorGoogleMaps.java | 2 +- ios/Plugin/Plugin.m | 2 +- ios/Plugin/Plugin.swift | 2 +- src/definitions.ts | 3 ++- src/interfaces/index.ts | 2 +- src/interfaces/methods/ViewBounds.ts | 25 +++++++++++++++++++ src/web.ts | 3 ++- 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java index 93c29f0f..a3ef606f 100644 --- a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java +++ b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java @@ -543,7 +543,7 @@ public void run() { } @PluginMethod() - public void viewBounds(final PluginCall call) { + public void getViewBounds(final PluginCall call) { final String mapId = call.getString("mapId"); getBridge().executeOnMainThread(new Runnable() { @Override diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index 50414be2..6da93eed 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -26,5 +26,5 @@ CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback); - CAP_PLUGIN_METHOD(viewBounds, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getViewBounds, CAPPluginReturnPromise); ) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index ea840e22..6cdedb1f 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -295,7 +295,7 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { setCallbackIdForEvent(call: call, eventName: CustomMapView.EVENT_DID_END_MOVING_CAMERA); } - @objc func viewBounds(_ call: CAPPluginCall) { + @objc func getViewBounds(_ call: CAPPluginCall) { let mapId: String = call.getString("mapId", "") DispatchQueue.main.async { diff --git a/src/definitions.ts b/src/definitions.ts index c0e53694..aee3aa2a 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -17,6 +17,7 @@ import { AddMarkersResult, RemoveMarkerOptions, ViewBoundsOptions, + ViewBoundsResult, // events DidTapInfoWindowCallback, DidCloseInfoWindowCallback, @@ -72,7 +73,7 @@ export interface CapacitorGoogleMapsPlugin { removeMarker(options: RemoveMarkerOptions): Promise; - viewBounds(options: ViewBoundsOptions): Promise; + getViewBounds(options: ViewBoundsOptions): Promise; didTapInfoWindow( options: DefaultEventOptions, diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index c7faa469..f019e94d 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -9,7 +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 } from './methods/ViewBounds'; +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 index 08085844..29070c54 100644 --- a/src/interfaces/methods/ViewBounds.ts +++ b/src/interfaces/methods/ViewBounds.ts @@ -1,3 +1,4 @@ +import { LatLng } from "./../../definitions"; export interface ViewBoundsOptions { /** * The identifier of the map to which this method should be applied. @@ -5,4 +6,28 @@ export interface ViewBoundsOptions { * @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; +} +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 186eb0e6..dabf77f3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -14,6 +14,7 @@ import { ElementFromPointResultOptions, AddMarkerOptions, ViewBoundsOptions, + ViewBoundsResult, AddMarkerResult, AddMarkersOptions, AddMarkersResult, @@ -83,7 +84,7 @@ export class CapacitorGoogleMapsWeb throw this.unimplemented("Not implemented on web."); } - async viewBounds(_options: ViewBoundsOptions): Promise { + async getViewBounds(_options: ViewBoundsOptions): Promise { throw new Error('Method not implemented on web.'); } From 03f74c02b39cd8cacafee486a9daa2cf1965f20c Mon Sep 17 00:00:00 2001 From: Jason Jozwiak Date: Fri, 13 May 2022 11:46:19 -0400 Subject: [PATCH 3/4] Updates to viewBounds implementation Changing viewBounds to getViewBounds and adding ViewBoundsResult interface to provide a return type for the getViewBounds method --- .../CapacitorGoogleMaps.java | 2 +- ios/Plugin/Plugin.m | 2 +- ios/Plugin/Plugin.swift | 2 +- src/definitions.ts | 3 ++- src/interfaces/index.ts | 2 +- src/interfaces/methods/ViewBounds.ts | 25 +++++++++++++++++++ src/web.ts | 3 ++- 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java index 93c29f0f..a3ef606f 100644 --- a/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java +++ b/android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java @@ -543,7 +543,7 @@ public void run() { } @PluginMethod() - public void viewBounds(final PluginCall call) { + public void getViewBounds(final PluginCall call) { final String mapId = call.getString("mapId"); getBridge().executeOnMainThread(new Runnable() { @Override diff --git a/ios/Plugin/Plugin.m b/ios/Plugin/Plugin.m index 50414be2..6da93eed 100644 --- a/ios/Plugin/Plugin.m +++ b/ios/Plugin/Plugin.m @@ -26,5 +26,5 @@ CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback); CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback); - CAP_PLUGIN_METHOD(viewBounds, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getViewBounds, CAPPluginReturnPromise); ) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index ea840e22..6cdedb1f 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -295,7 +295,7 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { setCallbackIdForEvent(call: call, eventName: CustomMapView.EVENT_DID_END_MOVING_CAMERA); } - @objc func viewBounds(_ call: CAPPluginCall) { + @objc func getViewBounds(_ call: CAPPluginCall) { let mapId: String = call.getString("mapId", "") DispatchQueue.main.async { diff --git a/src/definitions.ts b/src/definitions.ts index c0e53694..aee3aa2a 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -17,6 +17,7 @@ import { AddMarkersResult, RemoveMarkerOptions, ViewBoundsOptions, + ViewBoundsResult, // events DidTapInfoWindowCallback, DidCloseInfoWindowCallback, @@ -72,7 +73,7 @@ export interface CapacitorGoogleMapsPlugin { removeMarker(options: RemoveMarkerOptions): Promise; - viewBounds(options: ViewBoundsOptions): Promise; + getViewBounds(options: ViewBoundsOptions): Promise; didTapInfoWindow( options: DefaultEventOptions, diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index c7faa469..f019e94d 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -9,7 +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 } from './methods/ViewBounds'; +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 index 08085844..29070c54 100644 --- a/src/interfaces/methods/ViewBounds.ts +++ b/src/interfaces/methods/ViewBounds.ts @@ -1,3 +1,4 @@ +import { LatLng } from "./../../definitions"; export interface ViewBoundsOptions { /** * The identifier of the map to which this method should be applied. @@ -5,4 +6,28 @@ export interface ViewBoundsOptions { * @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; +} +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 186eb0e6..dabf77f3 100644 --- a/src/web.ts +++ b/src/web.ts @@ -14,6 +14,7 @@ import { ElementFromPointResultOptions, AddMarkerOptions, ViewBoundsOptions, + ViewBoundsResult, AddMarkerResult, AddMarkersOptions, AddMarkersResult, @@ -83,7 +84,7 @@ export class CapacitorGoogleMapsWeb throw this.unimplemented("Not implemented on web."); } - async viewBounds(_options: ViewBoundsOptions): Promise { + async getViewBounds(_options: ViewBoundsOptions): Promise { throw new Error('Method not implemented on web.'); } From 634b3f6c87ec0f95089235953b47da6e43af3d52 Mon Sep 17 00:00:00 2001 From: Jason Jozwiak Date: Fri, 27 May 2022 15:59:00 -0400 Subject: [PATCH 4/4] return center coordinates too --- ios/Plugin/Plugin.swift | 6 ++++++ src/interfaces/methods/ViewBounds.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index 6cdedb1f..bc92acd5 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -305,6 +305,8 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { return } + let centerPoint = customMapView.GMapView.center + let centerCoords = customMapView.GMapView.projection.coordinate(for: centerPoint) let bounds = customMapView.GMapView.projection.visibleRegion(); call.resolve([ @@ -324,6 +326,10 @@ public class CapacitorGoogleMaps: CustomMapViewEvents { "nearRight":[ "latitude": bounds.nearRight.latitude as Any, "longitude": bounds.nearRight.longitude as Any + ], + "center":[ + "latitude": centerCoords.latitude as Any, + "longitude": centerCoords.longitude as Any ] ] ]) diff --git a/src/interfaces/methods/ViewBounds.ts b/src/interfaces/methods/ViewBounds.ts index 29070c54..7e11851c 100644 --- a/src/interfaces/methods/ViewBounds.ts +++ b/src/interfaces/methods/ViewBounds.ts @@ -24,6 +24,10 @@ export interface Bounds { * @since 2.0.0 */ nearRight: LatLng; + /** + * @since 2.0.0 + */ + center: LatLng; } export interface ViewBoundsResult { /**