我有一个ionic 3应用,可以在地图上显示标记。但是现在,我对应用程序提出了新的要求,将其移植到适用的设备(Android Wear和Apple Watch)上。在手表的地图上显示标记和一些信息...
经过大量搜索和对论坛的任何疑问,我得到了一个Android Wear插件https://github.com/tgardner/cordova-androidwear,但这是一个cordova插件。 科尔多瓦的代码是:
function watch(handle) {
var self = this;
AndroidWear.onDataReceived(handle, function(e) {
self.dataReceived(e.data);
});
self.handle = handle;
}
watch.prototype = {
dataReceived: function(data) {
app.logEvent("AndroidWear message received: " + data);
},
sendMessage: function(message) {
AndroidWear.sendData(this.handle, message);
app.logEvent("AndroidWear message sent!");
}
};
var app = {
watch: null,
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
var self = this;
document.addEventListener('deviceready', function() {
self.onDeviceReady();
}, false);
},
onDeviceReady: function() {
var self = this;
self.receivedEvent('deviceready');
if(AndroidWear) {
AndroidWear.onConnect(function(e) {
self.logEvent("AndroidWear connection established");
self.watch = new watch(e.handle);
});
}
var sendButton = document.getElementById("sendMessage");
sendButton.addEventListener("click", function() {
if(self.watch) {
self.watch.sendMessage("Mensaje");
}
});
},
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
this.logEvent('Received Event: ' + id);
},
logEvent: function(message) {
var events = document.getElementById("events");
var el = document.createElement("li");
el.innerHTML = message;
events.appendChild(el);
}
};
但是我需要将cordova代码转换为离子代码。 我尝试:
(<any>window).AndroidWear.onConnect(() =>{
console.log("Androidwear : CONECTADO " );
})
(<any>window).AndroidWear.onDataReceived((data) =>{
console.log("Plugin: " + data);
})
但是我收到这样的执行错误 [INFO:CONSOLE(13355)]“未设置导航根:TypeError:window.AndroidWear.onConnect(...)不是函数”,来源:file:///android_asset/www/build/main.js (13355)
我从来没有将Cordova代码转换为ionic 3,所以也许我有愚蠢的错误。
如何翻译该代码以在ionic 3应用程序上使用android-wear插件?
谢谢。