Skip to content

Commit

Permalink
Adding viewBounds function to return map view bounds of all corners i…
Browse files Browse the repository at this point in the history
…n Lat/Lng. Based on contribution by @J-Gonzalez PR capacitor-community#81 but updated to match project structure now being used in main branch
  • Loading branch information
jjozwiak committed May 7, 2022
1 parent 2206512 commit bfefc37
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,45 @@ public void run() {
}
});
}

@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");
}

}
});
}
}
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
CAP_PLUGIN_METHOD(didBeginMovingCamera, CAPPluginReturnCallback);
CAP_PLUGIN_METHOD(didMoveCamera, CAPPluginReturnCallback);
CAP_PLUGIN_METHOD(didEndMovingCamera, CAPPluginReturnCallback);
CAP_PLUGIN_METHOD(viewBounds, CAPPluginReturnPromise);
)
35 changes: 35 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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", "")

Expand Down
3 changes: 3 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AddMarkerOptions,
AddMarkerResult,
RemoveMarkerOptions,
ViewBoundsOptions,
// events
DidTapInfoWindowCallback,
DidCloseInfoWindowCallback,
Expand Down Expand Up @@ -67,6 +68,8 @@ export interface CapacitorGoogleMapsPlugin {

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

viewBounds(options: ViewBoundsOptions): Promise<any>;

didTapInfoWindow(
options: DefaultEventOptions,
callback: DidTapInfoWindowCallback
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { MoveCameraOptions } from "./methods/MoveCamera";
export { ElementFromPointResultOptions } from "./methods/ElementFromPointResult";
export { AddMarkerOptions, AddMarkerResult } from "./methods/AddMarker";
export { RemoveMarkerOptions } from "./methods/RemoveMarker";
export { ViewBoundsOptions } from './methods/ViewBounds';

// events
export * from "./events/DidTapInfoWindow";
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/methods/ViewBounds.ts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MoveCameraOptions,
ElementFromPointResultOptions,
AddMarkerOptions,
ViewBoundsOptions,
AddMarkerResult,
RemoveMarkerOptions,
DidTapInfoWindowCallback,
Expand Down Expand Up @@ -76,6 +77,10 @@ export class CapacitorGoogleMapsWeb
throw this.unimplemented("Not implemented on web.");
}

async viewBounds(_options: ViewBoundsOptions): Promise<any> {
throw new Error('Method not implemented on web.');
}

async didTapInfoWindow(
_options: DefaultEventOptions,
_callback: DidTapInfoWindowCallback
Expand Down

0 comments on commit bfefc37

Please sign in to comment.