Skip to content

Commit bfefc37

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 2206512 commit bfefc37

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,45 @@ public void run() {
516516
}
517517
});
518518
}
519+
520+
@PluginMethod()
521+
public void viewBounds(final PluginCall call) {
522+
final String mapId = call.getString("mapId");
523+
getBridge().executeOnMainThread(new Runnable() {
524+
@Override
525+
public void run() {
526+
527+
CustomMapView customMapView = customMapViews.get(mapId);
528+
529+
if (customMapView != null) {
530+
JSObject result = new JSObject();
531+
JSObject bounds = new JSObject();
532+
JSObject farLeft = new JSObject();
533+
JSObject farRight = new JSObject();
534+
JSObject nearLeft = new JSObject();
535+
JSObject nearRight = new JSObject();
536+
537+
farLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.latitude);
538+
farLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farLeft.longitude);
539+
farRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.latitude);
540+
farRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().farRight.longitude);
541+
nearLeft.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.latitude);
542+
nearLeft.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearLeft.longitude);
543+
nearRight.put("latitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.latitude);
544+
nearRight.put("longitude", customMapView.googleMap.getProjection().getVisibleRegion().nearRight.longitude);
545+
546+
bounds.put("farLeft",farLeft);
547+
bounds.put("farRight",farRight);
548+
bounds.put("nearLeft",nearLeft);
549+
bounds.put("nearRight",nearRight);
550+
result.put("bounds",bounds);
551+
552+
call.resolve(result);
553+
} else {
554+
call.reject("map not found");
555+
}
556+
557+
}
558+
});
559+
}
519560
}

ios/Plugin/Plugin.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback);
2626
CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback);
2727
CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback);
28+
CAP_PLUGIN_METHOD(viewBounds, CAPPluginReturnPromise);
2829
)

ios/Plugin/Plugin.swift

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

252+
@objc func viewBounds(_ call: CAPPluginCall) {
253+
let mapId: String = call.getString("mapId", "")
254+
255+
DispatchQueue.main.async {
256+
257+
guard let customMapView = self.customWebView?.customMapViews[mapId] else {
258+
call.reject("map not found")
259+
return
260+
}
261+
262+
let bounds = customMapView.GMapView.projection.visibleRegion();
263+
264+
call.resolve([
265+
"bounds":[
266+
"farLeft": [
267+
"latitude": bounds.farLeft.latitude as Any,
268+
"longitude": bounds.farLeft.longitude as Any
269+
],
270+
"farRight":[
271+
"latitude": bounds.farRight.latitude as Any,
272+
"longitude": bounds.farRight.longitude as Any
273+
],
274+
"nearLeft":[
275+
"latitude": bounds.nearLeft.latitude as Any,
276+
"longitude": bounds.nearLeft.longitude as Any
277+
],
278+
"nearRight":[
279+
"latitude": bounds.nearRight.latitude as Any,
280+
"longitude": bounds.nearRight.longitude as Any
281+
]
282+
]
283+
])
284+
}
285+
}
286+
252287
func setCallbackIdForEvent(call: CAPPluginCall, eventName: String) {
253288
let mapId: String = call.getString("mapId", "")
254289

src/definitions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
AddMarkerOptions,
1515
AddMarkerResult,
1616
RemoveMarkerOptions,
17+
ViewBoundsOptions,
1718
// events
1819
DidTapInfoWindowCallback,
1920
DidCloseInfoWindowCallback,
@@ -67,6 +68,8 @@ export interface CapacitorGoogleMapsPlugin {
6768

6869
removeMarker(options: RemoveMarkerOptions): Promise<void>;
6970

71+
viewBounds(options: ViewBoundsOptions): Promise<any>;
72+
7073
didTapInfoWindow(
7174
options: DefaultEventOptions,
7275
callback: DidTapInfoWindowCallback

src/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { MoveCameraOptions } from "./methods/MoveCamera";
88
export { ElementFromPointResultOptions } from "./methods/ElementFromPointResult";
99
export { AddMarkerOptions, AddMarkerResult } from "./methods/AddMarker";
1010
export { RemoveMarkerOptions } from "./methods/RemoveMarker";
11+
export { ViewBoundsOptions } from './methods/ViewBounds';
1112

1213
// events
1314
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
RemoveMarkerOptions,
1819
DidTapInfoWindowCallback,
@@ -76,6 +77,10 @@ export class CapacitorGoogleMapsWeb
7677
throw this.unimplemented("Not implemented on web.");
7778
}
7879

80+
async viewBounds(_options: ViewBoundsOptions): Promise<any> {
81+
throw new Error('Method not implemented on web.');
82+
}
83+
7984
async didTapInfoWindow(
8085
_options: DefaultEventOptions,
8186
_callback: DidTapInfoWindowCallback

0 commit comments

Comments
 (0)