Javascript 如何通过 Google Maps API 检索纬度和经度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2770421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:51:49  来源:igfitidea点击:

How retrieve latitude and longitude via Google Maps API?

javascriptgoogle-maps

提问by Bored

How retrieve latitude and longitude via Google Maps API?

如何通过 Google Maps API 检索纬度和经度?

回答by Adnan Baliwala

With API v3

使用 API v3

google.maps.event.addListener(map, 'click', function(event) {
   alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});

Likewise you can retrieve when a marker is dragged dropped :

同样,您可以在拖放标记时检索:

google.maps.event.addListener(marker, 'dragstart', function(event) {
   alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});

回答by RedBlueThing

If you need to find out the latitude and longitude of an address using the Google Maps API, you need to use the Google Maps Geocoding Service:

如果您需要使用 Google Maps API 找出地址的纬度和经度,则需要使用Google Maps Geocoding Service

var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();

var address = "1600 Amphitheatre Parkway, Mountain  View";
geocoder.getLatLng(address, function(point) {
         var latitude = point.y;
         var longitude = point.x;  

         // do something with the lat lng
    });

If you would like to get the latitude and longitude of a position clicked on your Google map, you can do this in the click event:

如果你想在你的谷歌地图上获得一个位置的纬度和经度,你可以在点击事件中做到这一点:

GEvent.addListener(map, "click", function(marker,point) {
        var latitude = point.y;
        var longitude = point.x;

        // do something with the lat/lng
    });

回答by m82amjad

I have found the result via the following method:

我通过以下方法找到了结果:

http://maps.googleapis.com/maps/api/geocode/json?address=woking&sensor=false

http://maps.googleapis.com/maps/api/geocode/json?address=woking&sensor=false

  console.log(response.results[0].geometry.viewport.northeast.lat);
  console.log(response.results[0].geometry.viewport.northeast.lng);

回答by Sergey

How about ? Working fine ...

怎么样 ?工作正常...

var map = new google.maps.Map(....);
var longitude = map.center.lng();
var latitude = map.center.lat();

or

或者

var longitude = map.getCenter().lng();
var latitude = map.getCenter().lat();

回答by darkphantom500

To add to RedBlueThing's answer you can do stuff like:

要添加到 RedBlueThing 的答案,您可以执行以下操作:

geocoded_by :address

def address
 [street, city, state, country].compact.join(', ')
end

to help you narrow down the exact location, otherwise if you just send in the street the geocoder gem just returns the first instance of that street it finds which could be anywhere in the world

帮助您缩小确切位置的范围,否则如果您只是发送街道,地理编码器 gem 只会返回该街道的第一个实例,该实例可能位于世界任何地方

回答by varun

I am not sure if you are requesting a way of getting latitude and longitude via code(although you did say API :) ), If you are then there are a million posts out there which will show you how(including the ones above), but if you are interested in some kinda utility which just tells you where you are clicking , then Google-maps already comes with it. check out his post here on how to enable that functionality.

我不确定您是否要求通过代码获取纬度和经度的方法(尽管您确实说过 API :) ),如果您是,那么那里有一百万个帖子将向您展示如何(包括上面的那些),但是,如果您对某种只是告诉您点击位置的实用程序感兴趣,那么 Google-maps 已经附带了它。在此处查看他关于如何启用该功能的帖子。

http://varunpant.com/posts/find-longitude-and-latitude-in-google-maps

http://varunpant.com/posts/find-longitude-and-latitude-in-google-maps