简体   繁体   English

使用JavaScript中的Google Maps API查找当前位置

[英]Find current location using Google Maps API in JavaScript

I have been trying to use Google Maps API in one of my projects and the problem is it's not showing me the current exact location even after allowing the location access (it's a Website Project). 我一直在尝试在我的一个项目中使用Google Maps API,问题是即使允许位置访问(这是一个网站项目),它也没有向我显示当前的确切位置 I mean it's outputting the location but like 200 metres away. 我的意思是,它输出的是位置,但距离200米。 When I try to use Google Maps' web version on the same device it's showing me the current location +-20 metres. 当我尝试在同一台设备上使用Google Maps的网络版本时,会向我显示当前位置+ -20米。 We are trying to find the exact longitude and latitude. 我们正在尝试找到确切的经度和纬度。 Here is the JavaScript 这是JavaScript

function getCurrentLocation() {
    var  options = {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    }
}
// Try HTML5 geolocation.
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };
        var lat_and_long = pos.lat+","+pos.lng;
        $("#geocomplete").geocomplete("find", lat_and_long);
    }
}

$("#geocomplete").bind("geocode:dragged", function(event, latLng){
    $("input[name=lat]").val(pos.lat());
    $("input[name=lng]").val(pos.lng());
    $("#reset").show();
    console.log(latLng);
});
console.log(pos);

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
            'Error: The Geolocation service failed.' :
            'Error: Your browser doesn\'t support geolocation.');
        infoWindow.open(map);
}

We are using the following Google API with jQuery Geocoding and Places Autocomplete Plugin (1.7.0) https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyDJ2bLO-XXXXXXX" 我们正在将以下Google API与jQuery地理编码和Places自动完成插件(1.7.0)配合使用https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyDJ2bLO-XXXXXXX"

Google maps API gives locations based on below sources : Google Maps API根据以下来源提供位置:

  1. GPS : Gives you location by using satellites (up to around 20 meters) sometimes its inaccurate like bad weather conditions, underground or if you are you're inside buildings. GPS :通过使用卫星(最长约20米)为您提供位置信息,有时天气条件不佳(例如恶劣的天气),地下或如果您在建筑物内,它的位置都不准确。

  2. Wi-Fi : The location of Wi-Fi networks IP address. Wi-Fi :Wi-Fi网络IP地址的位置。

  3. Cell tower : Your connection to a mobile cellular network tower can be accurate up to a few thousand meters. 蜂窝塔 :您与移动蜂窝网络塔的连接可以精确到数千米。

DESKTOP BROWSER : while browse through desktop browsers like Mozilla Firefox and Google Chrome location rely on local network information, signals such as your computer's IP address . 桌面浏览器:在通过Mozilla Firefox和Google Chrome位置之类的桌面浏览器浏览时,依赖于本地网络信息,例如计算机IP address信号。 your browsers will ask you to share your location, if you allow to share then information about nearby wireless access points and IP address will share to Google Location Services to get an estimate of your location. 您的浏览器会要求您共享位置,如果允许共享,则有关附近无线接入点和IP address将共享给Google定位服务,以估算您的位置。 The accuracy of location will vary by location. 位置的准确性会因位置而异。

MOBILE BROWSER : Mobile browsers like Chrome,Safari,Opera use GPS sensors rather than WiFi network. 移动浏览器: Chrome,Safari,Opera等移动浏览器使用GPS sensors而非WiFi网络。

now enableHighAccuracy option attribute provides a hint that the application would like to receive the best possible results. 现在enableHighAccuracy选项属性提供了一个提示,表明应用程序希望获得最佳结果。 This MAY result in slower response times or increased power consumption. 这可能会导致响应时间变慢或功耗增加。

according to W3C's Geolocation API doc ( https://w3c.github.io/geolocation-api/#high-accuracy ) No guarantee is given that the API returns the device's actual location . 根据W3C的Geolocation API文档( https://w3c.github.io/geolocation-api/#high-accuracy ), No guarantee is given that the API returns the device's actual location

The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. Geolocation API为仅与托管实施的设备相关联的位置信息定义了高级接口,例如纬度和经度。 The API itself is agnostic of the underlying location information sources. API本身与基础位置信息源无关。 Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. 位置信息的常见来源包括全球定位系统(GPS)和从网络信号(例如IP地址,RFID,WiFi和蓝牙MAC地址,GSM / CDMA小区ID以及用户输入)推断出的位置。 No guarantee is given that the API returns the device's actual location. 无法保证API返回设备的实际位置。

There is no way to get the exact location. 无法获取确切位置。 Each location method contains errors. 每个定位方法都包含错误。 GPS is the most accurate, but the error is still in several meters range. GPS是最精确的,但误差仍在几米范围内。


In JavaScript, your best option is to use 在JavaScript中,最好的选择是使用

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };
        // ...
    } 
}

The Coordinates.accuracy read-only property is a strictly positive double representing the accuracy, with a 95% confidence level, of the Coordinates.latitude and Coordinates.longitude properties expressed in meters. Coordinates.accuracy只读属性是一个严格的正双精度值,表示以米为单位的Coordinates.latitudeCoordinates.longitude属性的准确性(置信度为95%)。

See details here . 在这里查看详细信息。

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

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