Skip to content

Commit 7aa39d8

Browse files
committed
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
1 parent 965d74c commit 7aa39d8

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

android/src/main/java/com/hemangkumar/capacitorgooglemaps/CapacitorGoogleMaps.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,48 @@ public void run() {
537537
} else {
538538
call.reject("map not found");
539539
}
540+
541+
}
542+
});
543+
}
544+
545+
@PluginMethod()
546+
public void viewBounds(final PluginCall call) {
547+
final String mapId = call.getString("mapId");
548+
getBridge().executeOnMainThread(new Runnable() {
549+
@Override
550+
public void run() {
551+
552+
CustomMapView customMapView = customMapViews.get(mapId);
553+
554+
if (customMapView != null) {
555+
JSObject result = new JSObject();
556+
JSObject bounds = new JSObject();
557+
JSObject farLeft = new JSObject();
558+
JSObject farRight = new JSObject();
559+
JSObject nearLeft = new JSObject();
560+
JSObject nearRight = new JSObject();
561+
562+
farLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.latitude);
563+
farLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.longitude);
564+
farRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.latitude);
565+
farRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.longitude);
566+
nearLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.latitude);
567+
nearLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.longitude);
568+
nearRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.latitude);
569+
nearRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.longitude);
570+
571+
bounds.put("farLeft",farLeft);
572+
bounds.put("farRight",farRight);
573+
bounds.put("nearLeft",nearLeft);
574+
bounds.put("nearRight",nearRight);
575+
result.put("bounds",bounds);
576+
577+
call.resolve(result);
578+
} else {
579+
call.reject("map not found");
580+
}
581+
540582
}
541583
});
542584
}

ios/Plugin/Plugin.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback);
2727
CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback);
2828
CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback);
29+
CAP_PLUGIN_METHOD(viewBounds, CAPPluginReturnPromise);
2930
)

ios/Plugin/Plugin.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,41 @@ public class CapacitorGoogleMaps: CustomMapViewEvents {
295295
setCallbackIdForEvent(call: call, eventName: CustomMapView.EVENT_DID_END_MOVING_CAMERA);
296296
}
297297

298+
@objc func viewBounds(_ call: CAPPluginCall) {
299+
let mapId: String = call.getString("mapId", "")
300+
301+
DispatchQueue.main.async {
302+
303+
guard let customMapView = self.customWebView?.customMapViews[mapId] else {
304+
call.reject("map not found")
305+
return
306+
}
307+
308+
let bounds = customMapView.GMapView.projection.visibleRegion();
309+
310+
call.resolve([
311+
"bounds":[
312+
"farLeft": [
313+
"latitude": bounds.farLeft.latitude as Any,
314+
"longitude": bounds.farLeft.longitude as Any
315+
],
316+
"farRight":[
317+
"latitude": bounds.farRight.latitude as Any,
318+
"longitude": bounds.farRight.longitude as Any
319+
],
320+
"nearLeft":[
321+
"latitude": bounds.nearLeft.latitude as Any,
322+
"longitude": bounds.nearLeft.longitude as Any
323+
],
324+
"nearRight":[
325+
"latitude": bounds.nearRight.latitude as Any,
326+
"longitude": bounds.nearRight.longitude as Any
327+
]
328+
]
329+
])
330+
}
331+
}
332+
298333
func setCallbackIdForEvent(call: CAPPluginCall, eventName: String) {
299334
let mapId: String = call.getString("mapId", "")
300335

src/definitions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
AddMarkersOptions,
1717
AddMarkersResult,
1818
RemoveMarkerOptions,
19+
ViewBoundsOptions,
1920
// events
2021
DidTapInfoWindowCallback,
2122
DidCloseInfoWindowCallback,
@@ -71,6 +72,8 @@ export interface CapacitorGoogleMapsPlugin {
7172

7273
removeMarker(options: RemoveMarkerOptions): Promise<void>;
7374

75+
viewBounds(options: ViewBoundsOptions): Promise<any>;
76+
7477
didTapInfoWindow(
7578
options: DefaultEventOptions,
7679
callback: DidTapInfoWindowCallback

src/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { ElementFromPointResultOptions } from "./methods/ElementFromPointResult"
99
export { AddMarkerOptions, AddMarkerResult } from "./methods/AddMarker";
1010
export { AddMarkersOptions, MarkerInputEntry, AddMarkersResult } from "./methods/AddMarkers";
1111
export { RemoveMarkerOptions } from "./methods/RemoveMarker";
12+
export { ViewBoundsOptions } from './methods/ViewBounds';
1213

1314
// events
1415
export * from "./events/DidTapInfoWindow";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface ViewBoundsOptions {
2+
/**
3+
* The identifier of the map to which this method should be applied.
4+
*
5+
* @since 2.0.0
6+
*/
7+
mapId: string;
8+
}

src/web.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
MoveCameraOptions,
1414
ElementFromPointResultOptions,
1515
AddMarkerOptions,
16+
ViewBoundsOptions,
1617
AddMarkerResult,
1718
AddMarkersOptions,
1819
AddMarkersResult,
@@ -82,6 +83,10 @@ export class CapacitorGoogleMapsWeb
8283
throw this.unimplemented("Not implemented on web.");
8384
}
8485

86+
async viewBounds(_options: ViewBoundsOptions): Promise<any> {
87+
throw new Error('Method not implemented on web.');
88+
}
89+
8590
async didTapInfoWindow(
8691
_options: DefaultEventOptions,
8792
_callback: DidTapInfoWindowCallback

0 commit comments

Comments
 (0)