Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding viewBounds function to return map view bounds of all corners i… #171

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

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

Expand Down
4 changes: 4 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
AddMarkersOptions,
AddMarkersResult,
RemoveMarkerOptions,
ViewBoundsOptions,
ViewBoundsResult,
// events
DidTapInfoWindowCallback,
DidCloseInfoWindowCallback,
Expand Down Expand Up @@ -71,6 +73,8 @@ export interface CapacitorGoogleMapsPlugin {

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

getViewBounds(options: ViewBoundsOptions): Promise<ViewBoundsResult>;

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 @@ -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";
Expand Down
37 changes: 37 additions & 0 deletions src/interfaces/methods/ViewBounds.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
MoveCameraOptions,
ElementFromPointResultOptions,
AddMarkerOptions,
ViewBoundsOptions,
ViewBoundsResult,
AddMarkerResult,
AddMarkersOptions,
AddMarkersResult,
Expand Down Expand Up @@ -82,6 +84,10 @@ export class CapacitorGoogleMapsWeb
throw this.unimplemented("Not implemented on web.");
}

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

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