11

我正在使用 Autocomplete 方法来获取 Place Suggestions,当我单击要选择的 Place 时,我想提取LatLng放置在place.geometry.location下面的位置。

萤火虫截图

根据我的观察,每个会话的键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);

    });
});
4

1 回答 1

39

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

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

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

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

于 2013-03-09T20:20:51.980 回答