[英]phonegap cordova gelocation not showing out on phone
var app = { // Application Constructor initialize: function() { this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); }, // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function() { app.receivedEvent('deviceready'); }, // Update DOM on a Received Event 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;'); console.log('Received Event: ' + id); } }; var myLatlng = new google.maps.LatLng(37.3000, -120.4833); var mapOptions = { center: myLatlng, zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map"), mapOptions); var marker = new google.maps.Marker({ position: myLatlng, map: map, draggable: true, animation: google.maps.Animation.DROP }); navigator.geolocation.getCurrentPosition(function(pos) { map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)); var myLocation = new google.maps.Marker({ position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude), map: map, title: "My Location" }); });
<html lang="en"> <head> <meta charset="UTF-8"> <title>Geo Location</title> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0; padding: 0 } #map { height: 100% } </style> <script type="text/javascript" src="cordova.js"></script> <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyB1oJGDZP2lM3c1dnPBmAbPBOb_Rh4u8n0&sensor=true"></script> <script type="text/javascript" src="js/index.js"></script> </head> <body> asd <div id="map"></div> </body> </html>
當我運行這兩個文件時,谷歌地圖沒有出現在我的手機中,我不知道為什么,但是當在網絡瀏覽器中運行它時,我也安裝了cordova geolocation插件並添加了權限,但仍然無法使它工作。你幫我嗎?
@ user3711175,您有幾個問題。 @Joerg是正確的。 您將需要使用whitelist
系統。 您有兩(2)個問題。
更新-2015-12-23 :我誤讀了代碼。 #1不適用
### 1。 您在錯誤的上下文中使用了 this
document.addEventListener('deviceready', this.onDeviceReady, false);
您要這樣編寫 app.onDeviceReady
:
document.addEventListener('deviceready', app.onDeviceReady, false);
當您的EventListener最終觸發時,它不再位於您的 app
對象的上下文中。現在是全球性的。 有關詳細信息,請參見此SO帖子
whitelist
系統 此白名單工作表應有所幫助。
如何將Cordova / Phonegap應用到白名單系統
需要將以下內容添加到whitelist
以進行Google地圖制作 。
Google地圖需要以下域。
可以使用通配符( *
)。 閱讀文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.