【问题标题】:How to get Client location using Google Maps API v3?如何使用 Google Maps API v3 获取客户端位置?
【发布时间】:2010-10-15 08:52:01
【问题描述】:

如何使用 Google Maps API v3 获取客户端位置?我尝试了以下代码,但不断收到“google.loader.ClientLocation 为空或不是对象”错误。任何想法为什么??

if (google.loader.ClientLocation) {
            alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude);
        }

谢谢

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    试试这个:)

        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
        function initialize() {
            var loc = {};
            var geocoder = new google.maps.Geocoder();
            if(google.loader.ClientLocation) {
                loc.lat = google.loader.ClientLocation.latitude;
                loc.lng = google.loader.ClientLocation.longitude;
    
                var latlng = new google.maps.LatLng(loc.lat, loc.lng);
                geocoder.geocode({'latLng': latlng}, function(results, status) {
                    if(status == google.maps.GeocoderStatus.OK) {
                        alert(results[0]['formatted_address']);
                    };
                });
            }
        }
    
        google.load("maps", "3.x", {other_params: "sensor=false", callback:initialize});
    
        </script>
    

    【讨论】:

    【解决方案2】:

    有点晚了,但我正在忙着构建类似的东西,这里是获取当前位置的代码 - 请务必使用本地服务器进行测试。

    包括来自 CDN 的相关脚本:

        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap">
    

    HTML

    <div id="map"></div>
    

    CSS

    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    #map {
        height: 100%;
    }
    

    JS

    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 6
    });
    var infoWindow = new google.maps.InfoWindow({map: map});
    
    // Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };
    
            infoWindow.setPosition(pos);
            infoWindow.setContent('Location found.');
            map.setCenter(pos);
        }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
        });
    } else {
        // Browser doesn't support Geolocation
        handleLocationError(false, infoWindow, map.getCenter());
    }
    
    function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
    }
    

    演示

    https://jsfiddle.net/ToreanJoel/4ythpy02/

    【讨论】:

    • 它工作正常但不准确它有大约 200 米的不准确度我们如何使它更准确
    【解决方案3】:

    我无法让上面的代码工作。

    Google 在这里做了很好的解释: http://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation

    他们首先使用 W3C 地理定位方法,然后为旧版浏览器提供 Google.gears 后备方法。

    例子在这里:

    http://code.google.com/apis/maps/documentation/javascript/examples/map-geolocation.html

    【讨论】:

      【解决方案4】:

      无需自己实现。我可以推荐使用来自google-maps-utility-library-v3geolocationmarker

      【讨论】:

        【解决方案5】:

        看来您现在不需要反向地理编码,现在直接从 ClientLocation 获取地址:

        google.loader.ClientLocation.address.city
        

        【讨论】:

          猜你喜欢
          • 2011-03-19
          • 2013-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-27
          • 1970-01-01
          • 2016-09-03
          • 1970-01-01
          相关资源
          最近更新 更多