2017-01-23 57 views
-1

我的Google地圖JavaScript代碼顯示在桌面瀏覽器上,但不顯示在移動設備上(不是Apple或Android)。我知道還有其他類似的問題,我已經嘗試了答案,沒有運氣。 這是我試過的: 當頁面加載時,我調用了getLocation函數。然後我初始化地圖...沒有運氣。地理位置代碼在桌面瀏覽器上工作,但不在移動設備上

我得到的錯誤消息是:error.POSITION_UNAVAILABLE我在每個移動設備上都收到此消息。

這是我的代碼:

$(document).ready(function(){ 
google.maps.event.addDomListener(window, "load", getLocation); 
}); 

function getLocation() { 
if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(initMap, showError); 
} 
else { 
    alert("Geolocation is not supported by this browser."); 
} 
} 

function initMap(tipoUser,userId,locations) { 
var icn="img/mechanic.png"; 
var meIcn="img/me.png"; 

directionsService = new google.maps.DirectionsService; 

directionsDisplay = new google.maps.DirectionsRenderer({ 
    polylineOptions: { 
    strokeColor: "red" 
} 
}); 

map = new google.maps.Map(document.getElementById('map-canvas'), { 
zoom: 13, 
center: new google.maps.LatLng(53.529773,-113.509387), 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

directionsDisplay.setMap(map); 

var i; 

var infowindow = new google.maps.InfoWindow(); 

$.each(locations, function(index, element) { 
marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(parseFloat(element.longitud), parseFloat(element.latitud)), 
center: new google.maps.LatLng(53.529773,-113.509387), 
    map: map, 
    icon: icn, 
    optimized: false 
}); 
markers.push(marker); 

google.maps.event.addListener(marker, 'mousedown', (function(marker, i) {     
     watchId = navigator.geolocation.watchPosition(function(position){ 
     lat = position.coords["latitude"]; 
     lng = position.coords["longitude"]; 
     markerMe = new google.maps.Marker({position: {lat: lat, lng: lng}, map: map, icon:meIcn});   
     }); 

    return function() {  
    var distinationOrigin = new google.maps.LatLng(lat, lng); 
    var destinationMarker = parseFloat(element.longitud) + ',' + '' + parseFloat(element.latitud); 
    infowindow.setContent(element.datos); 
    infowindow.open(map, marker); 
    calculateAndDisplayRoute(directionsService, directionsDisplay, distinationOrigin, destinationMarker, infowindow); 
    } 
})(marker, i)); 
}); 

var checkPositions=setInterval(function(){ 
    while(markers.length){ 
     markers.pop().setMap(null); 
    } 


$.post('codigo/getLatLngFromDB.php', {who:tipoUser, userId:userId},  function(data){ 
    miJson=$.parseJSON(data); 
    watchMecs(userId,miJson); 
    return false; 
}); 
},60000); 
} 


function showError(error) { 
    switch (error.code) { 
     case error.PERMISSION_DENIED: 
      alert("User denied the request for Geolocation."); 
      break; 
     case error.POSITION_UNAVAILABLE: 
      alert("Location information is unavailable."); 
      break; 
     case error.TIMEOUT: 
      alert("The request to get user location timed out."); 
      break; 
     case error.UNKNOWN_ERROR: 
      alert("An unkown error occurred."); 
      break; 
    } 
    } 
+0

是在特定瀏覽器中允許的位置服務嗎?你有沒有檢查設置?如果你想用正確的錯誤處理函數實現getLocation函數,你就會知道問題出在哪 –

+1

你在桌面上測試過哪些瀏覽器?你的網站是https嗎? – sideroxylon

+0

您的initMap函數具有錯誤的簽名作爲'navigator.geolocation.getCurrentPosition'的回調函數。即使在桌面瀏覽器中,發佈的代碼也不適用於我。請張貼演示您的問題的[mcve]。 – geocodezip

回答

0

我得到了解決它,

我不得不去谷歌的開發者網站,關鍵是,在許多方面,如*。實例添加我的域名.com .example.com/www.example.com www.example.com/等...這是我可以看到在手機上顯示的地圖的唯一途徑。

謝謝大家的意見

相關問題