google-maps - Google 地方信息获取 LatLng

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

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

Firebug Screenshot

根据我的观察,键 ibjb 在每次 session 中都会不断变化。有什么方法可以预测提取 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);

    });
});

最佳答案

place.geometry.locationLatLng ,因此您可以调用其 .lat().lng() 方法。

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

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

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

关于google-maps - Google 地方信息获取 LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15315017/

相关文章:

Javascript GooglMaps API 句柄经常不工作

iphone - 在 iPhone 应用程序的 map 上通过适当的图标显示酒店(和附近的地方)

android - 在 map View 上平滑地动画化相机路径

javascript - 谷歌街景图像API : calculate (automatically set) heading for given LatLng

javascript - 使用 Google map API 不显示 map 标记

android - 我可以在编辑文本中使用 Google Places API 吗?

ios - Google Places API 获取坐标

google-maps - Google Maps API v3 - GIcon 未定义

iphone - 谷歌地图 api 的替代品

javascript - 如何从 Google map 对象中隐藏公交车?