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

(feature) minZoom / maxZoom preference + getRegionInfo #237

Open
wants to merge 8 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 @@ -4,7 +4,6 @@
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +18,7 @@
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.VisibleRegion;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -278,7 +278,7 @@ public void run() {

call.resolve(result);
} else {
call.reject("map not found");
call.reject("updateMap: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -298,7 +298,7 @@ public void run() {

call.resolve(result);
} else {
call.reject("map not found");
call.reject("getMap: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -318,7 +318,7 @@ public void run() {
customMapViews.remove(mapId);
call.resolve();
} else {
call.reject("map not found");
call.reject("removeMap: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -327,17 +327,18 @@ public void run() {
@PluginMethod(returnType = PluginMethod.RETURN_NONE)
public void clearMap(final PluginCall call) {
final String mapId = call.getString("mapId");
final boolean hide = Boolean.TRUE.equals(call.getBoolean("hide", false));

getBridge().getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
CustomMapView customMapView = customMapViews.get(mapId);

if (customMapView != null) {
customMapView.clear();
customMapView.clear(hide);
call.resolve();
} else {
call.reject("map not found");
call.reject("clearMap: map not found, mapId: " + mapId);
}
}
});
Expand Down Expand Up @@ -371,7 +372,60 @@ public void run() {

call.resolve();
} else {
call.reject("map not found");
call.reject("moveCamera: map not found, mapId: " + mapId);
}
}
});
}

@PluginMethod()
public void getRegionInfo(final PluginCall call) {
final String mapId = call.getString("mapId");

getBridge().getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
CustomMapView customMapView = customMapViews.get(mapId);

if (customMapView != null) {
VisibleRegion region = customMapView.googleMap.getProjection().getVisibleRegion();

JSObject topLeft = new JSObject();
JSObject topRight = new JSObject();
JSObject bottomLeft = new JSObject();
JSObject bottomRight = new JSObject();

topLeft.put("latitude", region.latLngBounds.northeast.latitude);
topLeft.put("longitude", region.latLngBounds.southwest.longitude);

topRight.put("latitude", region.latLngBounds.northeast.latitude);
topRight.put("longitude", region.latLngBounds.northeast.longitude);

bottomLeft.put("latitude", region.latLngBounds.southwest.latitude);
bottomLeft.put("longitude", region.latLngBounds.southwest.longitude);

bottomRight.put("latitude", region.latLngBounds.southwest.latitude);
bottomRight.put("longitude", region.latLngBounds.northeast.longitude);

JSObject bounds = new JSObject();
bounds.put("topLeft", topLeft);
bounds.put("topRight", topRight);
bounds.put("bottomLeft", bottomLeft);
bounds.put("bottomRight", bottomRight);

JSObject center = new JSObject();
center.put("latitude", region.latLngBounds.getCenter().latitude);
center.put("longitude", region.latLngBounds.getCenter().longitude);

CameraPosition camera = customMapView.googleMap.getCameraPosition();
JSObject result = new JSObject();
result.put("bounds", bounds);
result.put("center", center);
result.put("zoom", camera.zoom);

call.resolve(result);
} else {
call.reject("getRegionInfo: map not found, mapId: " + mapId);
}
}
});
Expand Down Expand Up @@ -500,7 +554,7 @@ public void run() {
}
);
} else {
call.reject("map not found");
call.reject("addMarker: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -511,7 +565,7 @@ public void addMarkers(final PluginCall call) {
final String mapId = call.getString("mapId");
CustomMapView customMapView = customMapViews.get(mapId);
if (customMapView == null) {
call.reject("map not found");
call.reject("addMarkers: map not found, mapId: " + mapId);
return;
}
try {
Expand Down Expand Up @@ -539,7 +593,7 @@ public void run() {

call.resolve();
} else {
call.reject("map not found");
call.reject("removeMarker: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -562,7 +616,7 @@ public void run() {
call.resolve(customPolygon.getResultForPolygon(polygon, mapId));
});
} else {
call.reject("map not found");
call.reject("addPolygon: map not found, mapId: " + mapId);
}
}
});
Expand All @@ -584,7 +638,7 @@ public void run() {

call.resolve();
} else {
call.reject("map not found");
call.reject("removePolygon: map not found, mapId: " + mapId);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.location.Location;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

Expand Down Expand Up @@ -359,6 +360,8 @@ public void createMap(String callbackId, BoundingRect boundingRect, MapCameraPos

GoogleMapOptions googleMapOptions = this.mapPreferences.generateGoogleMapOptions();
googleMapOptions.camera(this.mapCameraPosition.cameraPosition);
googleMapOptions.minZoomPreference(mapPreferences.minZoom);
googleMapOptions.maxZoomPreference(mapPreferences.maxZoom);

mapView = new MapView(activity, googleMapOptions);

Expand Down Expand Up @@ -444,9 +447,10 @@ public void removeFromView(ViewGroup parent) {
parent.removeView(mapView);
}

public void clear() {
public void clear(boolean hide) {
googleMap.clear();
markers.clear();
mapView.setVisibility(hide ? View.INVISIBLE : View.VISIBLE);
}

public void addMarker(CustomMarker customMarker, @Nullable Consumer<Marker> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class MapPreferences {
public MapPreferencesGestures gestures;
public MapPreferencesControls controls;
public MapPreferencesAppearance appearance;
public Integer minZoom;
public Integer maxZoom;

public MapPreferences() {
this.gestures = new MapPreferencesGestures();
Expand All @@ -27,6 +29,9 @@ public void updateFromJSObject(@Nullable JSObject preferences) {
// update appearance
JSObject appearanceObject = preferences.getJSObject("appearance");
this.appearance.updateFromJSObject(appearanceObject);
// update zoom
this.minZoom = preferences.getInteger("minZoom", 1);
this.maxZoom = preferences.getInteger("maxZoom", 21);
}
}

Expand Down
6 changes: 5 additions & 1 deletion ios/Plugin/CustomMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,20 @@ class CustomMapView: UIViewController, GMSMapViewDelegate {
self.GMapView.isMyLocationEnabled = self.mapPreferences.appearance.isMyLocationDotShown;
self.GMapView.isTrafficEnabled = self.mapPreferences.appearance.isTrafficShown;

// set zoom
self.GMapView.setMinZoom(self.mapPreferences.minZoom, maxZoom: self.mapPreferences.maxZoom);

return self.getResultForMap();
}

func getMap() -> PluginCallResultData {
return self.getResultForMap();
}

func clearMap() {
func clearMap(hide: Bool) {
if (self.GMapView != nil) {
self.GMapView.clear();
self.GMapView.isHidden = hide == true;
}
}

Expand Down
5 changes: 5 additions & 0 deletions ios/Plugin/MapPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MapPreferences {
public var gestures: MapPreferencesGestures!
public var controls: MapPreferencesControls!
public var appearance: MapPreferencesAppearance!
public var minZoom: Float!
public var maxZoom: Float!

public init() {
self.gestures = MapPreferencesGestures();
Expand All @@ -22,6 +24,9 @@ class MapPreferences {
// update appearance
let appearanceObject: JSObject! = preferences["appearance"] as? JSObject ?? JSObject();
self.appearance.updateFromJSObject(object: appearanceObject);
// update zoom
self.minZoom = preferences["minZoom"] as? Float ?? 1.0;
self.maxZoom = preferences["maxZoom"] as? Float ?? 21.0;
}
}
}
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CAP_PLUGIN_METHOD(clearMap, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(removeMap, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(moveCamera, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getRegionInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addMarker, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(addMarkers, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeMarker, CAPPluginReturnPromise);
Expand Down
45 changes: 43 additions & 2 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ public class CapacitorGoogleMaps: CustomMapViewEvents {

@objc func clearMap(_ call: CAPPluginCall) {
let mapId: String = call.getString("mapId", "")
let hide: Bool = call.getBool("hide", false)

DispatchQueue.main.async {
guard let customMapView = self.customWebView?.customMapViews[mapId] else {
call.reject("map not found")
return
}
let result = customMapView.clearMap()

let result = customMapView.clearMap(hide: hide)

call.resolve()
}
Expand Down Expand Up @@ -178,6 +179,46 @@ public class CapacitorGoogleMaps: CustomMapViewEvents {
}
}

@objc func getRegionInfo(_ 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 region = customMapView.GMapView.projection.visibleRegion();
let centerCoords = customMapView.GMapView.projection.coordinate(for: customMapView.GMapView.center)

call.resolve([
"bounds": [
"topLeft": [
"latitude": region.farLeft.latitude as Any,
"longitude": region.farLeft.longitude as Any
],
"topRight": [
"latitude": region.farRight.latitude as Any,
"longitude": region.farRight.longitude as Any
],
"bottomLeft": [
"latitude": region.nearLeft.latitude as Any,
"longitude": region.nearLeft.longitude as Any
],
"bottomRight": [
"latitude": region.nearRight.latitude as Any,
"longitude": region.nearRight.longitude as Any
]
],
"center": [
"latitude": centerCoords.latitude as Any,
"longitude": centerCoords.longitude as Any
],
"zoom": customMapView.GMapView.camera.zoom
])
}
}

@objc func addMarker(_ call: CAPPluginCall) {
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 @@ -10,6 +10,8 @@ import {
RemoveMapOptions,
ClearMapOptions,
MoveCameraOptions,
GetRegionInfoOptions,
GetRegionInfoResult,
ElementFromPointResultOptions,
AddMarkerOptions,
AddMarkerResult,
Expand Down Expand Up @@ -68,6 +70,8 @@ export interface CapacitorGoogleMapsPlugin {

moveCamera(options: MoveCameraOptions): Promise<void>;

getRegionInfo(options: GetRegionInfoOptions): Promise<GetRegionInfoResult>;

addMarker(options: AddMarkerOptions): Promise<AddMarkerResult>;

addMarkers(options: AddMarkersOptions): Promise<AddMarkersResult>;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { UpdateMapOptions, UpdateMapResult } from "./methods/UpdateMap";
export { RemoveMapOptions } from "./methods/RemoveMap";
export { ClearMapOptions } from "./methods/ClearMap";
export { MoveCameraOptions } from "./methods/MoveCamera";
export { GetRegionInfoOptions, GetRegionInfoResult } from './methods/GetRegionInfo';
export { ElementFromPointResultOptions } from "./methods/ElementFromPointResult";
export { AddMarkerOptions, AddMarkerResult } from "./methods/AddMarker";
export {
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/methods/ClearMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ export interface ClearMapOptions {
* @since 2.0.0
*/
mapId: string;
/**
* @since 2.0.0
* if set to true MapView will become invisible. default false.
*/
hide?: boolean;
}
Loading