【问题标题】:Google Places get LatLngGoogle 地方信息获取 LatLng
【发布时间】:2013-02-25 05:43:15
【问题描述】:

我正在使用自动完成方法来获取地点建议,当我点击我想要选择的地点时,我想提取位于place.geometry.location 下的LatLng,如下所示。

根据我的观察,键 ibjb 在每次会话中都会不断变化。有什么方法可以提取出LatLng

$(document).ready(function() {

    var mapOptions = {
        center : new google.maps.LatLng(-33.8688, 151.2195),
        zoom : 13,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    $('#searchTextField').bind('keydown keypress', function() {
        setTimeout(function() {
            var inputQuery = $('#searchTextField').val();

            if (inputQuery.length >= 2) {
                //console.log(inputQuery);

                /*
                var service = new google.maps.places.AutocompleteService();

                service.getPlacePredictions({
                    input : inputQuery
                }, callback);
                */

                var input = document.getElementById('searchTextField');
                var options = {
                    types : ['geocode']
                };

                var autocomplete = new google.maps.places.Autocomplete(input, options);

                // Acting on Selecting a place
                google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    //infowindow.close();
                    var place = autocomplete.getPlace();

                    console.log(place);
                    console.log(place.formatted_address);
                    console.log(place.name);
                    console.log(place.geometry.location);
                    console.log(place.geometry.location[0]);

                    // Show the map to the current location selected
                    if (place.geometry.viewport) {
                        map.fitBounds(place.geometry.viewport);
                    } else {
                        map.setCenter(place.geometry.location);
                        map.setZoom(17);
                        // Why 17? Because it looks good.
                    }

                    var marker = new google.maps.Marker({
                        position : place.geometry.location,
                        map : map,
                        draggable : true,
                    });

                    $.each(place.geometry.location, function(key, value) {
                        console.log(key + ": " + value);
                    }); 
                });
            }

        }, 0);

    });
});

【问题讨论】:

    标签: google-maps google-maps-api-3 google-places-api geocoding


    【解决方案1】:

    place.geometry.location 是一个LatLng,所以你可以调用它的.lat().lng() 方法。

    var location = place.geometry.location;
    var lat = location.lat();
    var lng = location.lng();
    

    您会看到这些方法存在于您对该对象的检查中。

    切勿使用未记录的属性,例如您发现的 ibjb。 Google 使用 Closure Compiler 或类似工具编译 Maps API,为私有属性和变量生成随机名称。

    【讨论】:

    • 很高兴,我很高兴这对您有所帮助。
    • 我正在使用places webservice api,如何从响应中获取lat long?
    • 这里的位置变量是什么我收到错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多