Skip to content

Commit 391847e

Browse files
committed
feat(maps)): Add native maps
1 parent fff300d commit 391847e

File tree

10 files changed

+175
-0
lines changed

10 files changed

+175
-0
lines changed

config.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@
6565
<plugin name="cordova.plugins.diagnostic" spec="~3.4.1" />
6666
<plugin name="cordova-plugin-geolocation" spec="~2.4.2" />
6767
<plugin name="card.io.cordova.mobilesdk" spec="https://github.com/card-io/card.io-Cordova-Plugin" />
68+
<plugin name="cordova-plugin-googlemaps" spec="~1.4.0">
69+
<variable name="API_KEY_FOR_ANDROID" value="AIzaSyBaTVlHDndpSgbdDnRsCy2xFJt2tB41NB0" />
70+
<variable name="API_KEY_FOR_IOS" value="AIzaSyBaTVlHDndpSgbdDnRsCy2xFJt2tB41NB0" />
71+
</plugin>
6872
</widget>

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@ionic-native/core": "3.10.3",
2525
"@ionic-native/diagnostic": "3.10.3",
2626
"@ionic-native/geolocation": "3.10.3",
27+
"@ionic-native/google-maps": "^3.12.1",
2728
"@ionic-native/splash-screen": "3.10.3",
2829
"@ionic-native/status-bar": "3.10.3",
2930
"@ionic/storage": "2.0.1",
@@ -38,6 +39,7 @@
3839
},
3940
"devDependencies": {
4041
"@ionic/app-scripts": "1.3.7",
42+
"@ionic/cli-plugin-cordova": "1.4.0",
4143
"@ionic/cli-plugin-ionic-angular": "1.3.1",
4244
"typescript": "~2.3.3"
4345
},

src/app/app.imports.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { SlideColorChangingPage } from '../pages/slide/slide-color-changing/slid
2020
import { ToastService } from '../providers/util/toast.service';
2121
import { AlertService } from '../providers/util/alert.service';
2222
import { CameraProvider } from '../providers/util/camera.provider';
23+
import { NativeGoogleMapsProvider } from '../providers/native-google-maps/native-google-maps';
2324

2425
// Ionic native providers
2526
import { CardIO } from '@ionic-native/card-io';
@@ -29,6 +30,7 @@ import { Diagnostic } from '@ionic-native/diagnostic';
2930
import { Geolocation } from '@ionic-native/geolocation';
3031
import { StatusBar } from '@ionic-native/status-bar';
3132
import { SplashScreen } from '@ionic-native/splash-screen';
33+
import { GoogleMaps } from '@ionic-native/google-maps';
3234

3335
// Directives
3436
import { SlidingDrawer } from '../components/sliding-drawer/sliding-drawer';
@@ -85,6 +87,7 @@ export const Providers = [
8587
ToastService,
8688
AppState,
8789
CameraProvider,
90+
NativeGoogleMapsProvider,
8891

8992
// Ionic native specific providers
9093
BarcodeScanner,
@@ -94,6 +97,7 @@ export const Providers = [
9497
CardIO,
9598
StatusBar,
9699
SplashScreen,
100+
GoogleMaps,
97101
]
98102

99103
export const Components = [

src/pages/ionic-native/ionic-native.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class IonicNativePage {
2727
title: 'Get image from camera/gallery',
2828
page: 'GetImagePage'
2929
},
30+
{
31+
title: 'Google Maps',
32+
page: 'NativeGoogleMapsPage'
33+
},
3034
{
3135
title: 'Runtime permissions',
3236
page: 'RuntimePermissionsPage'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
ionic cordova plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="AIzaSyBaTVlHDndpSgbdDnRsCy2xFJt2tB41NB0" --variable API_KEY_FOR_IOS="AIzaSyBaTVlHDndpSgbdDnRsCy2xFJt2tB41NB0" && npm install --save @ionic-native/google-maps
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<ion-header>
2+
<ion-navbar>
3+
<ion-title>NativeGoogleMaps</ion-title>
4+
</ion-navbar>
5+
</ion-header>
6+
7+
<ion-content text-center>
8+
<div #map id="map" style="height: 100%"></div>
9+
</ion-content>
10+
<ion-footer>
11+
<button ion-button
12+
no-margin large
13+
full color="secondary"
14+
(click)="addMarker()">
15+
Add Marker
16+
</button>
17+
</ion-footer>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from '@angular/core';
2+
import { IonicPageModule } from 'ionic-angular';
3+
import { NativeGoogleMapsPage } from './native-google-maps';
4+
5+
@NgModule({
6+
declarations: [
7+
NativeGoogleMapsPage,
8+
],
9+
imports: [
10+
IonicPageModule.forChild(NativeGoogleMapsPage),
11+
],
12+
exports: [
13+
NativeGoogleMapsPage
14+
]
15+
})
16+
export class NativeGoogleMapsPageModule {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
page-native-google-maps {
2+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NativeGoogleMapsProvider } from '../../../providers/native-google-maps/native-google-maps';
2+
import { Component, ViewChild, ElementRef } from '@angular/core';
3+
import { IonicPage, NavController } from 'ionic-angular';
4+
5+
@IonicPage()
6+
@Component({
7+
selector: 'page-native-google-maps',
8+
templateUrl: 'native-google-maps.html',
9+
})
10+
export class NativeGoogleMapsPage {
11+
12+
@ViewChild('map') mapElement: ElementRef;
13+
14+
constructor(
15+
public navCtrl: NavController,
16+
public mapsCtrl: NativeGoogleMapsProvider) {
17+
}
18+
19+
// Load map only after view is initialized
20+
ngAfterViewInit() {
21+
this.mapsCtrl.create(this.mapElement).then( () => {
22+
this.mapsCtrl.centerToGeolocation();
23+
});
24+
}
25+
26+
addMarker(){
27+
this.mapsCtrl.addMarkerToGeolocation('Click me!', this.callbackSample);
28+
}
29+
30+
callbackSample() {
31+
alert('The callback was called :D');
32+
}
33+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { Injectable, ElementRef } from '@angular/core';
2+
3+
import {
4+
GoogleMaps,
5+
GoogleMap,
6+
GoogleMapsEvent,
7+
LatLng,
8+
CameraPosition,
9+
MarkerOptions,
10+
GoogleMapsAnimation
11+
} from '@ionic-native/google-maps';
12+
13+
import { Geolocation } from '@ionic-native/geolocation';
14+
15+
@Injectable()
16+
export class NativeGoogleMapsProvider {
17+
map: GoogleMap;
18+
19+
constructor(
20+
public geolocation: Geolocation,
21+
private googleMaps: GoogleMaps) {
22+
}
23+
24+
// Note: Call this method on ngAfterViewInit
25+
create(element: ElementRef) {
26+
const options = {
27+
backgroundColor: 'white',
28+
controls: {
29+
compass: true,
30+
myLocationButton: true,
31+
indoorPicker: true,
32+
zoom: true
33+
},
34+
gestures: {
35+
scroll: true,
36+
tilt: true,
37+
rotate: true,
38+
zoom: true
39+
}
40+
};
41+
42+
this.map = this.googleMaps.create(element.nativeElement, options);
43+
return this.map.one(GoogleMapsEvent.MAP_READY);
44+
}
45+
46+
centerToGeolocation(){
47+
return this.getGeolocationPosition().then((position) => {
48+
return this.centerToPosition(position);
49+
}, error => {
50+
return Promise.reject(error);
51+
});
52+
}
53+
54+
getGeolocationPosition(){
55+
return new Promise((resolve, reject) => {
56+
this.geolocation.getCurrentPosition().then((position) => {
57+
let latLng: LatLng = new LatLng(position.coords.latitude, position.coords.longitude);
58+
resolve(latLng);
59+
}, error => {
60+
reject(error);
61+
});
62+
});
63+
}
64+
65+
centerToPosition(latLng: any, zoom?: number, tilt?: number){
66+
let cameraPosition: CameraPosition = {
67+
target: latLng,
68+
zoom : zoom || 18,
69+
tilt : tilt || 10
70+
};
71+
return this.map.moveCamera(cameraPosition);
72+
}
73+
74+
addMarker(position, title: string, infoClickCallback, animated = true){
75+
let markerOptions: MarkerOptions = {
76+
position: position,
77+
title: title,
78+
animation: animated ? GoogleMapsAnimation.BOUNCE : null,
79+
infoClick: infoClickCallback
80+
};
81+
82+
return this.map.addMarker(markerOptions);
83+
}
84+
85+
addMarkerToGeolocation(title: string, infoClickCallback, animated?: boolean) {
86+
this.getGeolocationPosition().then(position => {
87+
this.addMarker(position, title, infoClickCallback, animated);
88+
});
89+
}
90+
}

0 commit comments

Comments
 (0)