mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
feat(native): starting native plugins
This commit is contained in:
48
ionic/components/app/test/native/index.ts
Normal file
48
ionic/components/app/test/native/index.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import {Component} from 'angular2/angular2';
|
||||||
|
import {Control, ControlGroup} from 'angular2/forms';
|
||||||
|
|
||||||
|
import {App, Http, Camera, Geolocation} from 'ionic/ionic';
|
||||||
|
|
||||||
|
let testUrl = 'https://ionic-api-tester.herokuapp.com/json';
|
||||||
|
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
|
||||||
|
|
||||||
|
|
||||||
|
@App({
|
||||||
|
templateUrl: 'main.html'
|
||||||
|
})
|
||||||
|
class IonicApp {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
doGetLocation() {
|
||||||
|
console.log('Getting location');
|
||||||
|
this.gettingLocation = true;
|
||||||
|
Geolocation.getCurrentPosition().then((pos) => {
|
||||||
|
this.gettingLocation = false;
|
||||||
|
console.log('Got location', pos);
|
||||||
|
this.location = pos;
|
||||||
|
}, (err) => {
|
||||||
|
this.gettingLocation = false;
|
||||||
|
console.error('Unable to get location', err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
doTrackLocation() {
|
||||||
|
this.gettingTrackLocation = true;
|
||||||
|
Geolocation.watchPosition().source.subscribe((pos) => {
|
||||||
|
this.gettingTrackLocation = false;
|
||||||
|
console.log('Got location', pos);
|
||||||
|
this.trackLocation = pos;
|
||||||
|
}, (err) => {
|
||||||
|
this.gettingTrackLocation = false;
|
||||||
|
console.error('Unable to get location', pos);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
getPicture() {
|
||||||
|
Camera.getPicture({
|
||||||
|
|
||||||
|
}).then(data => {
|
||||||
|
console.log('Data', data);
|
||||||
|
}, err => {
|
||||||
|
alert('Unable to take picture')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
17
ionic/components/app/test/native/main.html
Normal file
17
ionic/components/app/test/native/main.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<ion-view>
|
||||||
|
<ion-content padding>
|
||||||
|
<h2>Camera</h2>
|
||||||
|
<button primary (click)="getPicture()">Get Picture</button>
|
||||||
|
<h2>Geolocation</h2>
|
||||||
|
<button primary (click)="doGetLocation()">Get Location</button>
|
||||||
|
<div>
|
||||||
|
<b *ng-if="gettingLocation">Fetching location...</b>
|
||||||
|
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
|
||||||
|
</div>
|
||||||
|
<button primary (click)="doTrackLocation()">Track Location</button>
|
||||||
|
<div>
|
||||||
|
<b *ng-if="gettingTrackLocation">Fetching location...</b>
|
||||||
|
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
|
||||||
|
</div>
|
||||||
|
</ion-content>
|
||||||
|
</ion-view>
|
@ -23,3 +23,5 @@ export * from './animations/builtins'
|
|||||||
export * from './transitions/transition'
|
export * from './transitions/transition'
|
||||||
export * from './transitions/ios-transition'
|
export * from './transitions/ios-transition'
|
||||||
export * from './transitions/md-transition'
|
export * from './transitions/md-transition'
|
||||||
|
|
||||||
|
export * from './native/plugins'
|
||||||
|
38
ionic/native/camera/camera.ts
Normal file
38
ionic/native/camera/camera.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import * as util from 'ionic/util';
|
||||||
|
|
||||||
|
export class Camera {
|
||||||
|
static getPicture(options) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!navigator.camera) {
|
||||||
|
console.warn('Camera: no camera plugin installed. Pictures will not work.')
|
||||||
|
resolve(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var options = util.defaults({
|
||||||
|
quality: 80,
|
||||||
|
destinationType: window.Camera.DestinationType.DATA_URL,
|
||||||
|
sourceType: window.Camera.PictureSourceType.CAMERA,
|
||||||
|
allowEdit: true,
|
||||||
|
encodingType: window.Camera.EncodingType.JPEG,
|
||||||
|
popoverOptions: window.CameraPopoverOptions,
|
||||||
|
saveToPhotoAlbum: false
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
navigator.camera.getPicture(function (imageData) {
|
||||||
|
resolve(imageData);
|
||||||
|
}, function (err) {
|
||||||
|
reject(err);
|
||||||
|
}, options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
static cleanup() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
navigator.camera.cleanup(function () {
|
||||||
|
resolve();
|
||||||
|
}, function (err) {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
39
ionic/native/geolocation/geolocation.ts
Normal file
39
ionic/native/geolocation/geolocation.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import * as util from 'ionic/util';
|
||||||
|
|
||||||
|
import * as Rx from 'rx';
|
||||||
|
|
||||||
|
export class Geolocation {
|
||||||
|
static getCurrentPosition(options) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
navigator.geolocation.getCurrentPosition(function (result) {
|
||||||
|
resolve(result);
|
||||||
|
}, function (err) {
|
||||||
|
reject(err);
|
||||||
|
}, options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static watchPosition(options) {
|
||||||
|
let watchID;
|
||||||
|
|
||||||
|
let source = Rx.Observable.create((observer) => {
|
||||||
|
watchID = navigator.geolocation.watchPosition(function (result) {
|
||||||
|
observer.onNext(result)
|
||||||
|
}, function(err) {
|
||||||
|
observer.onError(err, observer);
|
||||||
|
}, options);
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
source: source,
|
||||||
|
watchID: watchID,
|
||||||
|
clear: () => {
|
||||||
|
navigator.geolocation.clearWatch(watchID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static clearWatch(watchID) {
|
||||||
|
return navigator.geolocation.clearWatch(watchID);
|
||||||
|
}
|
||||||
|
}
|
2
ionic/native/plugins.ts
Normal file
2
ionic/native/plugins.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from './camera/camera'
|
||||||
|
export * from './geolocation/geolocation'
|
Reference in New Issue
Block a user