GoogleMap class
May 2, 2020 ยท View on GitHub
This class extends BaseClass.
Contents
Overview
Create a map
You can create a google maps (map instance) using GoogleMaps.create() after the deviceready event.
import { Platform } from 'ionic-angular';
import {
GoogleMaps,
GoogleMap,
GoogleMapsEvent
} from '@ionic-native/google-maps';
export class MapPage {
map: GoogleMap;
constructor(private platform: Platform) {
this.platform.ready().then(() => {
this.loadMap();
});
}
loadmap() {
this.map = GoogleMaps.create('map_canvas');
// The `MAP_READY` event notifies the native map view is fully ready.
this.map.one(GoogleMapsEvent.MAP_READY).then(this.onMapReady.bind(this));
}
onMapReady() {
console.log('map is ready!');
}
}
How does the plugin work?
Before using this plugin, please understand how this plugin work.
-
App background becomes
transparentThis plugin embeds
native Google Maps viewunder the browser view. Because of this, this plugin changes the application background astransparent. If you want to set the background color, you need to use Environment.setBackgroundColor().
-
Manipulate like a HTML element!
The biggest benefit of this plugin is you can put/cover any HTML elements on the map, just like HTML!
The maps plugin recognizes all touches before browser, calculates who should receive the touch event, then pass it.
-
What's camera?
The native Google Map APIs have a 3D map internally. We can only see the map through the camera.
Events
-
User interface events
A map instance tells us what UI changes are occurred through these events:
- MAP_READY
- MAP_CLICK
- MAP_LONG_CLICK
- CAMERA_MOVE_START (including map dragging)
- CAMERA_MOVE_MOVE
- CAMERA_MOVE_END
- MY_LOCATION_BUTTON_CLICK
- MY_LOCATION_CLICK
- POI_CLICK
The above events passes with event information in parameter, such as where is clicked.
map.on(GoogleMapsEvent.MAP_CLICK).subscribe((params: any[]) => { let latLng: LatLng = params[0]; alert(latLng + " is clicked!"); });And also, all events passes the map instance to the last argument. This allows you to write your event listener code in a separate place.
map.on(GoogleMapsEvent.MAP_CLICK).subscribe(this.onMapClick.bind(this)); onMapClick(params: any[]) { let latLng: LatLng = params[0]; let map: GoogleMap = params[1]; // <-- You can get the target of MAP_CLICK event map.addMarker({ position: latLng }); } -
MVC status change events
The
mapclass extends BaseClass. TheBaseClasshasset()andget()methods. This is very useful methods.If you set a value through
set()method, you can listen the(key)_changedevent.The benefit of this is you can write your code
Model-View-Controllerseparately.map.on(GoogleMapsEvent.MAP_CLICK).subscribe((params: any[]) => { let latLng: LatLng = params[0]; map.set("clickPosition", latLng); }); map.on("clickPosition_changed").subscribe((params: any[]) => { let oldValue: LatLng = params[0]; let newValue: LatLng = params[1]; let target: Map = params[2]; // clickPosition is even with newValue let clickPosition: LatLng = target.get("clickPosition"); });
Camera
-
Changing the camera position
This plugin has two basic methods:
map.animateCamera()changes the camera position with animation.map.moveCamera()changes the camera position without animation.
map.animateCamera({ target: {lat: 37.422359, lng: -122.084344}, zoom: 17, tilt: 60, bearing: 140, duration: 5000 }).then(() => { //alert("Camera target has been changed"); });Other methods
animateCameraZoomIn(),moveCameraZoomIn(),setCameraTarget()...etc use one of the above methods internally.Since this plugin stops other methods execution during the camera moving, this is bad performance code.
Bad performance code
map.setCameraZoom(10); map.setCameraTilt(30); map.setCameraBearing(45);Recommended way is using one method as much as possible.
Good performance code
map.moveCamera({ zoom: 10, tilt: 30, bearing: 45 }); -
Get the camera position
You can get the camera position through these method anytime.
map.getCameraPosition()// Returnscenter,zoom,bearing, andtiltvaluesmap.getCameraZoom()map.getCameraTarget()map.getCameraBearing()map.getCameraTilt()
Also you can listen the
CAMERA_MOVEevents.map.on(GoogleMapsEvent.CAMERA_MOVE).subscribe((params: any[]) => { let cameraPosition: CameraPosition<LatLng> = params[0]; console.log(cameraPosition.target); });If you want to listen particular property, you can do like this:
map.on('camera_target_changed').subscribe((params: any[]) => { let latLng: LatLng = params[0]; console.log("map center = " + latLng); });
API Reference
Class methods
-
addMarker(options) :orange_book:
Adds a marker.
Params Type Details options MarkerOptions options :arrow_right: Returns
Promise<Marker> -
addMarkerSync(options) :orange_book:
Adds a marker synchronously.
Params Type Details options MarkerOptions options :arrow_right: Returns
Marker -
addMarkerCluster(options) :orange_book:
Adds a marker cluster.
Params Type Details options MarkerClusterOptions options :arrow_right: Returns
Promise<MarkerCluster> -
addMarkerClusterSync(options) :orange_book:
Adds a marker cluster synchronously.
Params Type Details options MarkerClusterOptions options :arrow_right: Returns
Promise<MarkerCluster> -
addCircle(options) :orange_book:
Adds a circle.
Params Type Details options CircleOptions options :arrow_right: Returns
Promise<Circle> -
addCircleSync(options) :orange_book:
Adds a circle synchronously.
Params Type Details options CircleOptions options :arrow_right: Returns
Circle -
addPolygon(options) :orange_book:
Adds a polygon.
Params Type Details options PolygonOptions options :arrow_right: Returns
Promise<Polygon> -
addPolygonSync(options) :orange_book:
Adds a polygon synchronously.
Params Type Details options PolygonOptions options :arrow_right: Returns
Polygon -
addPolyline(options) :orange_book:
Adds a polyline
Params Type Details options PolylineOptions options :arrow_right: Returns
Promise<Polyline> -
addPolylineSync(options) :orange_book:
Adds a polyline synchronously.
Params Type Details options PolylineOptions options :arrow_right: Returns
Polyline -
addTileOverlay(options) :orange_book:
Adds a tile overlay
Params Type Details options TileOverlayOptions options :arrow_right: Returns
Promise<TileOverlay> -
addTileOverlaySyc(options) :orange_book:
Adds a tile overlay synchronously.
Params Type Details options TileOverlayOptions options :arrow_right: Returns
TileOverlay -
addGroundOverlay(options) :orange_book:
Adds a ground overlay synchronously.
Params Type Details options GroundOverlayOptions options :arrow_right: Returns
GroundOverlay -
addGroundOverlaySync(options) :orange_book:
Adds a ground overlay
Params Type Details options GroundOverlayOptions options :arrow_right: Returns
Promise<GroundOverlay> -
addKmlOverlay(options) :orange_book:
Adds a kml overlay
Params Type Details options KmlOverlayOptions options :arrow_right: Returns
Promise<KmlOverlay> -
setDiv(domNode?) :orange_book:
If you want to display the map in an html element, you need to specify an element or id. If omit this argument, the map is detached from webview.
Params Type Details domNode string | HTMLElement (optional)Specifies the container of map div -
getDiv()
Returns the map HTML element
:arrow_right: Returns
HTMLElement -
getFocusedBuilding()
Get the currently focused building
:arrow_right: Returns
Promise<any> -
setMapTypeId(mapTypeId) :orange_book:
Changes the map type id. Available constants are one of the GoogleMapsMapTypeId
-
animateCamera(cameraPosition: CameraPosition
) :orange_book:Moves the camera with animation
Params Type Details cameraPosition CameraPosition<any> Specify camera options :arrow_right: Returns
Promise<any> -
animateCameraZoomIn() :orange_book:
Zooming in the camera with animation
:arrow_right: Returns
Promise<any> -
animateCameraZoomOut() :orange_book:
Zooming out the camera with animation
:arrow_right: Returns
Promise<any> -
moveCamera(cameraPosition: CameraPosition
) :orange_book:Moves the camera with animation
Params Type Details cameraPosition CameraPosition<any> Specify camera options
Note that the `duration` property is ignored.:arrow_right: Returns
Promise<any> -
moveCameraZoomIn() :orange_book:
Zooming in the camera without animation
:arrow_right: Returns
Promise<any> -
moveCameraZoomOut() :orange_book:
Zooming out the camera without animation
:arrow_right: Returns
Promise<any> -
getCameraPosition() :orange_book:
Get the position of the camera
:arrow_right: Returns
CameraPosition<ILatLng> -
getCameraTarget() :orange_book:
Get the current camera target position
:arrow_right: Returns
ILatLng -
getCameraZoom() :orange_book:
Get the current camera zoom level
:arrow_right: Returns
number -
getCameraBearing() :orange_book:
Get the current camera bearing
:arrow_right: Returns
number -
getCameraTilt() :orange_book:
Get the current camera tilt (view angle)
:arrow_right: Returns
number -
setCameraTarget(target) :orange_book:
Set the center position of the camera view. This is a wrapper of
moveCamera()method.Params Type Details target ILatLng | ILatLng[] Specify camera target -
setCameraZoom(zoomLevel) :orange_book:
Set zoom level of the camera. This is a wrapper of
moveCamera()method.Params Type Details zoomLevel number Specify zoom level -
setCameraTilt(tiltAngle) :orange_book:
Set the camera view angle. This is a wrapper of
moveCamera()method.Params Type Details tiltAngle number Specify tilt angle -
setCameraBearing(bearing) :orange_book:
Set camera bearing. This is a wrapper of
moveCamera()method.Params Type Details bearing number Specify tilt angle -
panBy(x, y) :orange_book:
Change the center of the map by the given distance in pixels.
Params Type Details x number Distance in pixel for x y number Distance in pixel for y -
getVisibleRegion() :orange_book:
Get the current visible region (southWest and northEast)
:arrow_right: Returns VisibleRegion
-
getMyLocation(options?) :orange_book:
Get the current device location
Params Type Details options MyLocationOptions (optional) options :arrow_right: Returns
Promise<MyLocation> -
setClickable(isClickable) :orange_book:
Set false to ignore all clicks on the map
Params Type Details isClickable boolean true / false -
setMyLocationEnabled(enabled) :orange_book:
Set true if you want to show the MyLocation control (blue dot)
Params Type Details enabled boolean true / false -
setMyLocationButtonEnabled(enabled) :orange_book:
Set true if you want to show the MyLocation button
Params Type Details enabled boolean true / false -
setTrafficEnabled(enabled) :orange_book:
Set true if you want to show the traffic layer
Params Type Details enabled boolean true / false -
setCompassEnabled(enabled) :orange_book:
Set true if you want to show the compass button
Params Type Details enabled boolean true / false -
setAllGesturesEnabled(enabled) :orange_book:
Sets the preference for whether all gestures should be enabled or disabled
Params Type Details enabled boolean true / false -
setVisible(visible) :orange_book:
Set visibility of the map
Params Type Details visible boolean true / false -
setIndoorEnabled(enabled) :orange_book:
Set true if you want to show the indoor map
Params Type Details enabled boolean true / false -
setPadding(top, right?, bottom?, left?) :orange_book:
Adjust the map padding (same as CSS padding rule)
Params Type Details top number padding of top in pixels right number (optional) padding of right in pixels bottom number (optional) padding of bottom in pixels left number (optional) padding of left in pixels -
setOptions(options) :orange_book:
Set options
Params Type Details options GoogleMapOptions map options -
remove() :orange_book:
Destroy a map completely
:arrow_right: Returns
Promise<void> -
clear() :orange_book:
Remove all overlays, such as marker
:arrow_right: Returns
Promise<void> -
fromLatLngToPoint(latLng) :orange_book:
Convert the unit from LatLng to the pixels from the left/top of the map div.
Params Type Details latLng ILatLng a geographic point :arrow_right: Returns
Promise<any[]> -
fromPointToLatLng(point) :orange_book:
Convert the unit from the pixels from the left/top to the LatLng.
Params Type Details point point: number[] a pixel point :arrow_right: Returns
Promise<LatLng> -
toDataURL(options?) :orange_book:
Convert the unit from the pixels from the left/top to the LatLng.
Params Type Details options ToDataUrlOptions (optional) options :arrow_right: Returns
Promise<string>
Event
-
MAP_READY :orange_book:
This event is fired when the native map view is fully ready.
Params Type Details params[0] GoogleMap map instance -
MAP_CLICK :orange_book:
This event is fired when you click on the map.
Params Type Details params[0] LatLng clicked position params[1] GoogleMap map instance -
MAP_LONG_CLICK :orange_book:
This event is fired when you long-click on the map.
Params Type Details params[0] LatLng clicked position params[1] GoogleMap map instance -
MY_LOCATION_BUTTON_CLICK :orange_book:
This event is fired when you click on the myLocation button.
Params Type Details params[0] GoogleMap map instance -
MY_LOCATION_CLICK :orange_book:
This event is fired when you click on the myLocation control (blue dot).
Params Type Details params[0] LatLng clicked position params[1] GoogleMap map instance -
POI_CLICK :orange_book:
This event is fired when you tap on POIs(such as building icon).
Params Type Details params[0] string place ID params[1] string place name params[2] GoogleMap map instance -
CAMERA_MOVE_START,CAMERA_MOVE,CAMERA_MOVE_END :orange_book:
These events are fired when the camera moves.
Params Type Details params[0] LatLng camera position params[1] GoogleMap map instance -
MAP_DRAG_START,MAP_DRAG,MAP_DRAG_END :orange_book:
These events are fired when the camera moves with gestures.
Params Type Details params[0] GoogleMap map instance

