Javascript 从谷歌地图中的纬度和经度获取位置地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14051106/
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-24 15:34:25  来源:igfitidea点击:

Get location address from Latitude and Longitude in google maps

javascriptjquerygoogle-maps

提问by jennifer Jolie

I want after click on google map and getting Latitude and Longitude get location place from they and put it (address) in filed input#searchTextField. What do i do?

我想在点击谷歌地图并获取纬度和经度后从他们那里获取位置并将其(地址)放入归档中input#searchTextField。我该怎么办?

I tried as, but don't work for me:

我试过,但不适合我:

DEMO:http://jsfiddle.net/DXkZJ/

演示:http : //jsfiddle.net/DXkZJ/

<input id="searchTextField" type="text" size="50" style="text-align:    left;width:357px;direction: ltr;">
<div id="map_canvas" style="height: 350px;width: 500px;margin: 0.6em;"></div>



 $(function () {
     var lat = 44.88623409320778,
         lng = -87.86480712897173,
         latlng = new google.maps.LatLng(lat, lng),
         image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png';

     //zoomControl: true,
     //zoomControlOptions: google.maps.ZoomControlStyle.LARGE,

     var mapOptions = {
         center: new google.maps.LatLng(lat, lng),
         zoom: 13,
         mapTypeId: google.maps.MapTypeId.ROADMAP,
         panControl: true,
         panControlOptions: {
             position: google.maps.ControlPosition.TOP_RIGHT
         },
         zoomControl: true,
         zoomControlOptions: {
             style: google.maps.ZoomControlStyle.LARGE,
             position: google.maps.ControlPosition.TOP_left
         }
     },
     map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions),
         marker = new google.maps.Marker({
             position: latlng,
             map: map,
             icon: image
         });

     var input = document.getElementById('searchTextField');
     var autocomplete = new google.maps.places.Autocomplete(input, {
         types: ["geocode"]
     });

     autocomplete.bindTo('bounds', map);
     var infowindow = new google.maps.InfoWindow();

     google.maps.event.addListener(autocomplete, 'place_changed', function (event) {
         infowindow.close();
         var place = autocomplete.getPlace();
         if (place.geometry.viewport) {
             map.fitBounds(place.geometry.viewport);
         } else {
             map.setCenter(place.geometry.location);
             map.setZoom(17);
         }

         moveMarker(place.name, place.geometry.location);
         alert(place.name);
         $('.MapLat').val(place.geometry.location.lat());
         $('.MapLon').val(place.geometry.location.lng());
     });
     google.maps.event.addListener(map, 'click', function (event) {
         $('.MapLat').val(event.latLng.lat());
         $('.MapLon').val(event.latLng.lng());
         alert(event.latLng.place.name)
     });
     $("#searchTextField").focusin(function () {
         $(document).keypress(function (e) {
             if (e.which == 13) {
                 return false;
                 infowindow.close();
                 var firstResult = $(".pac-container .pac-item:first").text();
                 var geocoder = new google.maps.Geocoder();
                 geocoder.geocode({
                     "address": firstResult
                 }, function (results, status) {
                     if (status == google.maps.GeocoderStatus.OK) {
                         var lat = results[0].geometry.location.lat(),
                             lng = results[0].geometry.location.lng(),
                             placeName = results[0].address_components[0].long_name,
                             latlng = new google.maps.LatLng(lat, lng);

                         moveMarker(placeName, latlng);
                         $("input").val(firstResult);
                         alert(firstResult)
                     }
                 });
             }
         });
     });

     function moveMarker(placeName, latlng) {
         marker.setIcon(image);
         marker.setPosition(latlng);
         infowindow.setContent(placeName);
         //infowindow.open(map, marker);
     }
 });?

回答by kieran

Ok, so firstly you need reverse geocoding, not regular geocoding, because you are going from a latitude/longitude into an address rather than the other way around (see reference). Secondly your key listener would never do anything because it almost immediately states return false; in this case you probably want event.stopPropogation(). Furthermore google maps will intercept your mouse clicks at a greater depth in the DOM tree than document, so the event will never be listened that highly anyway.

好的,首先您需要反向地理编码,而不是常规地理编码,因为您要从纬度/经度转到地址,而不是相反(请参阅参考资料)。其次,您的主要听众永远不会做任何事情,因为它几乎立即说明return false;在这种情况下,您可能想要event.stopPropogation(). 此外,谷歌地图将在 DOM 树中比 更深入地拦截您的鼠标点击document,因此无论如何都不会高度侦听该事件。

So what I did was to change your geocode into a reverse geocode, as well as move the code in the document listener into the google listener. If you wanted to retain the behaviour of the listener being added on focus, you can simply create a new listener on the map object. Here is a working jsfiddle demonstrating these changes, hopefully this helps.

所以我所做的是将您的地理编码更改为反向地理编码,并将文档侦听器中的代码移动到谷歌侦听器中。如果您想保留被添加到焦点上的监听器的行为,您可以简单地在地图对象上创建一个新的监听器。这是一个演示这些更改有效jsfiddle,希望这会有所帮助。

回答by mohan.gade

Take a look at the gmap developers api. for topic Reverse Geocoding (Address Lookup). You will got your answer.

看看 gmap 开发人员 api。对于主题反向地理编码(地址查找)。你会得到你的答案。

回答by Darshit Gajjar

Just add this code after moveMarker() function

只需在 moveMarker() 函数之后添加此代码

$("#map_canvas").click(function() {          
    $("#searchTextField").val($(".MapLat").val() +", "+$(".MapLon").val());
});

Hope, this is what you needed :)

希望,这就是你需要的:)