简体   繁体   English

如何使用google maps api获得精确的位置?

[英]How can I get precise location with google maps api?

I'm trying to show the user's current location on map. 我正试图在地图上显示用户的当前位置。 Using the HTML5 geolocation it asks the user for permission, which I find annoying and I found I could bypass this by using directly the google maps api. 使用HTML5地理位置,它要求用户许可,我觉得很烦,我发现我可以通过直接使用谷歌地图api绕过这个。

The problem is that with either solution the accuracy can be well off (300km off). 问题在于,使用任何一种解决方案,准确度都可以很好(300km关闭)。

HTML5 solution HTML5解决方案

https://developers.google.com/maps/documentation/javascript/geolocation https://developers.google.com/maps/documentation/javascript/geolocation

This is the example that google provides and it is well off. 这是谷歌提供的例子,它很好。 Also the accuracy changes from browser to browser. 从浏览器到浏览器的准确度也会发生变化

Google maps api solution 谷歌地图api解决方案

This is the code I've reached to. 这是我达到的代码。

function calculateZoomLevel(accuracy) {
    var equatorLength = 40075004; // in meters
    var mapWidth = 480; // in pixels
    var metersPerPixel = equatorLength / 4096;
    var zoomLevel = 1;
    while ((metersPerPixel * mapWidth) > accuracy) {
        metersPerPixel /= 2;
        zoomLevel++;
    }
    return Math.min(zoomLevel, 17);
}
function initMap() {
    var coords = new google.maps.LatLng(37.990832, 23.70332), accuracy = 0;
    $.post('https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY').done(function(e) {
        coords = new google.maps.LatLng(e.location.lat, e.location.lng);
        accuracy = e.accuracy;
        map = new google.maps.Map(document.getElementById('map'), {
            zoom: calculateZoomLevel(accuracy),
            center: coords,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
        });
        new google.maps.Circle({
            center: coords,
            radius: accuracy,
            map: map,
            fillColor: '#ff0000',
            fillOpacity: .5,
            strokeColor: '#ff0000',
            strokeOpacity: .5
        });
    }).fail(function(e) {
        console.log('fail ajax post geolocation', e);
    });
}

The thing is, I know the precision can be quite good (10-30 meters), since at the same time and on the same browser I see myself somewhere totally off on google's example, I see myself at my spot in the normal google maps page. 问题是,我知道精度可以很好(10-30米),因为同时在同一个浏览器上我看到自己完全脱离谷歌的例子,我看到自己在普通谷歌地图的我的位置页。

So if you don't want to capture and calculate most possible position at the cost of a lot of requests or you don't want to make your own script to connect to this api which would be a challenge... Create a looping threshold. 因此,如果您不想以大量请求为代价捕获和计算大多数可能的位置,或者您不希望自己的脚本连接到此api,这将是一个挑战...创建一个循环阈值。 Recall your function if it is not a reasonable accuracy. 如果不合理的准确性,请回想一下您的功能。 Meaning: 含义:

   coords = new google.maps.LatLng(e.location.lat, e.location.lng);
   accuracy = e.accuracy;
var c = 0;
if (c<20){
     If (accuracy <100){
         .. do your stuff..
     }else{
         initMap();
         c++;
     }
}else{
     ...failed....
}

So word to the wise... This will increase your requests in obvious ways. 所以说明智......这会以明显的方式增加你的要求。 You have been warned..... Unless google allows unlimited requests for free when you read this. 你已被警告.....除非谷歌允许无限量的免费请求,当你读这篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM