使用Javascript从Google Places搜索api获取纬度和经度

时间:2022-08-22 10:19:14

How do I get the longitude and latitude from the searched location with the google maps place search box api.

如何使用谷歌地图搜索框api从搜索到的位置获取经度和纬度。

Im using the same code as the google demo - https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

我使用与google演示相同的代码 - https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

4 个解决方案

#1


25  

function GetLatlong()
    {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById('textboxid').value;

        geocoder.geocode({ 'address': address }, function (results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();

            }
        });

You can use the above function which will give you the latitude and longitude for the area entered by user.

您可以使用上述功能,该功能将为您提供用户输入区域的纬度和经度。

#2


13  

The code on the link you provide shows a function to do when a search is entered. First, it creates an empty array of markers (the pins you see on the map once you perform a search).

您提供的链接上的代码显示了输入搜索时要执行的功能。首先,它创建一个空的标记数组(一旦执行搜索,您在地图上看到的引脚)。

So, check the function starting with:

因此,检查以下开头的函数:

google.maps.event.addListener(searchBox, 'places_changed', function() {

google.maps.event.addListener(searchBox,'places_changed',function(){

You'll see later on that a marker has been created (there's even a comment):

稍后您将看到已创建标记(甚至还有注释):

// Create a marker for each place.
var marker = new google.maps.Marker({
    map: map,
    icon: image,
    title: place.name,
    position: place.geometry.location
});

So, on place.geometry.location you have a Google Maps Location object. You could use place.geometry.location.lat() and place.geometry.location.lng().

因此,在place.geometry.location上,您有一个Google Maps Location对象。您可以使用place.geometry.location.lat()和place.geometry.location.lng()。

Check here, too: https://*.com/a/15315483/1012139

请点击这里:https://*.com/a/15315483/1012139

#3


1  

I think you might want to look into the Google Maps geocoding service

我想您可能想要查看Google地图地理编码服务

#4


0  

From the docs:

来自文档:

                // Autocomplete Options
                var defaultBounds = new google.maps.LatLngBounds();
                var options = {
                types: ['(cities)'],
                bounds: defaultBounds
                };

                // get DOM's input element
                var input = document.getElementById('location_address');

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

                // Listener for whenever input value changes            
                autocomplete.addListener('place_changed', function() {

                    // Get place info
                    var place = autocomplete.getPlace();

                    // Do whatever with the value!
                    console.log(place.geometry.location.lat());
                });

#1


25  

function GetLatlong()
    {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById('textboxid').value;

        geocoder.geocode({ 'address': address }, function (results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();

            }
        });

You can use the above function which will give you the latitude and longitude for the area entered by user.

您可以使用上述功能,该功能将为您提供用户输入区域的纬度和经度。

#2


13  

The code on the link you provide shows a function to do when a search is entered. First, it creates an empty array of markers (the pins you see on the map once you perform a search).

您提供的链接上的代码显示了输入搜索时要执行的功能。首先,它创建一个空的标记数组(一旦执行搜索,您在地图上看到的引脚)。

So, check the function starting with:

因此,检查以下开头的函数:

google.maps.event.addListener(searchBox, 'places_changed', function() {

google.maps.event.addListener(searchBox,'places_changed',function(){

You'll see later on that a marker has been created (there's even a comment):

稍后您将看到已创建标记(甚至还有注释):

// Create a marker for each place.
var marker = new google.maps.Marker({
    map: map,
    icon: image,
    title: place.name,
    position: place.geometry.location
});

So, on place.geometry.location you have a Google Maps Location object. You could use place.geometry.location.lat() and place.geometry.location.lng().

因此,在place.geometry.location上,您有一个Google Maps Location对象。您可以使用place.geometry.location.lat()和place.geometry.location.lng()。

Check here, too: https://*.com/a/15315483/1012139

请点击这里:https://*.com/a/15315483/1012139

#3


1  

I think you might want to look into the Google Maps geocoding service

我想您可能想要查看Google地图地理编码服务

#4


0  

From the docs:

来自文档:

                // Autocomplete Options
                var defaultBounds = new google.maps.LatLngBounds();
                var options = {
                types: ['(cities)'],
                bounds: defaultBounds
                };

                // get DOM's input element
                var input = document.getElementById('location_address');

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

                // Listener for whenever input value changes            
                autocomplete.addListener('place_changed', function() {

                    // Get place info
                    var place = autocomplete.getPlace();

                    // Do whatever with the value!
                    console.log(place.geometry.location.lat());
                });