Google地方信息获得LatLng [英] Google Places get LatLng

查看:68
本文介绍了Google地方信息获得LatLng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动完成"方法来获取位置建议,当我单击要选择的位置时,我要提取放置在place.geometry.location下的LatLng,如下所示.

I am using the Autocomplete method to get Place Suggestions and when I click the Place I want to choose, I want to extract the Lat and Lng which is placed under place.geometry.location as below.

根据我的观察,键ibjb在每次会话中都会不断变化.有什么方法可以预测地提取LatLng吗?

As per my observation, the keys ib and jb keep changing with every session. Is there any way I can extract the Lat and Lng predictably?

$(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.location您将在检查此对象时看到这些方法.

You'll see that these methods are present in your inspection of this object.

切勿使用未发现的属性,例如发现的ibjb. Google使用关闭编译器或类似的工具编译Maps API,该工具会为私有文件生成随机名称属性和变量.

Never use undocumented properties such as the ib and jb you discovered. Google compiles the Maps API using the Closure Compiler or a similar tool, which generates random names for private properties and variables.

这篇关于Google地方信息获得LatLng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆