簡體   English   中英

地理位置html5無法在Oppo和Honor Android手機上使用

[英]Geolocation html5 not working for Oppo and Honor Android phones

這是我用來構建Google Maps的代碼,該代碼可在80%的Android手機中使用,但本地電話制造商不支持此代碼,因為我無法在OPPO,HONOR等手機中獲取坐標。

我嘗試了Google Geolocation API,但沒有給出確切的信息。

 function inti () { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(geoLocationSucess); backview = "views/home.html"; } } function writeAddress(latLng) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({ "location": latLng }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { var rr = results[0].formatted_address; var res = rr.split(","); // alert(res[0] + ", " +res[1]+ "," + res[2]); } else "Unable to retrieve your address" + "<br />"; }); } function initialize() { //checker to watch position every sec if position fixed it will not refresh if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(geoLocationSucess, showError); } else alert("Your Device does't support geolocation."); } function geoLocationSucess(position) { var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); writeAddress(userLatLng) var myOptions = { zoom: 15, center: userLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var mapObject = new google.maps.Map(document.getElementById("googleMap"), myOptions); var marker = new google.maps.Marker({ map: mapObject, position: userLatLng }); var sunCircle = { strokeColor: "#7DB1E8", strokeOpacity: 0.5, strokeWeight: 2, fillColor: "#7DB1E8", fillOpacity: 0.25, map: mapObject, center: userLatLng, radius: 200 // in meters }; var cityCircle = new google.maps.Circle(sunCircle); cityCircle.bindTo(center, marker); } function showError(error) { switch (error.code) { case 1: alert("User denied the request for Geolocation."); break; case 2: alert("Location information is unavailable. Please ensure Location is On"); break; case 3: break; case 4: alert("An unknown error occurred."); break; } } 
 <!DOCTYPE html> <html> <style> #googleMap { width: 100%; height: 80vh; } </style> <body> <div id="googleMap"> </div> <span id="locate" data-role="touch"><a data-role="button" onclick="initialize()" id="locate" style=" width:50%;background:#18515C;" >Locate Me </a></span> <span id="punch" data-role="touch"><a data-role="button" onclick="punch()" id="punch" style="width:40%;background:#18515C; ">Punch </a></span> <script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script> </body> </html> 

嘗試這個

<p><button onclick="geoFindMe()">Show my location</button></p>
<div id="out"></div>

腳本

function geoFindMe() {
  var output = document.getElementById("out");

  if (!navigator.geolocation){
    output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
    return;
  }

  function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';

    var img = new Image();
    img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";

    output.appendChild(img);
  }

  function error() {
    output.innerHTML = "Unable to retrieve your location";
  }

  output.innerHTML = "<p>Locating…</p>";

  navigator.geolocation.getCurrentPosition(success, error);
}

如果這不起作用,請使用cordova地理位置插件,然后嘗試。

嘗試使用地理位置選項。

結果將受到以下原因的影響,

  • 行動版Webkit版本
  • 手機上網速度
  • 該設備可以清晰無阻地看到天空。 GPS偵聽來自多個衛星的非常微弱的信號。 這些信號可能會被建築物,茂密的樹葉,車頂等部分或全部遮擋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM