如何使用谷歌地图API获取当前位置名称 [英] how to get current postion name using google map api

查看:646
本文介绍了如何使用谷歌地图API获取当前位置名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 google map api,它可以获取 current 纬度经度中c $ c>用户位置但相反,我想获取位置名称

例如,我的位置是 bangalore => vidyaranyapura 我想获得 vidyaranyapura



这里是demo: https://jsfiddle.net/aoe4vf17/100/

html :

javascript:

  var map = new google.maps.Map(document.getElementById(map),{
center:new google.maps.LatLng(53.3478,-6.2597) ,
zoom:16,
mapTypeId:google.maps.MapTypeId.ROADMAP
});

var infoWindow = new google.maps.InfoWindow({
map:map
});
//尝试HTML5地理位置。
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var pos = {
lat:position.coords.latitude,
lng:position.coords.longitude
};
console.log(position);
infoWindow.setPosition(pos);
infoWindow.setContent('< b> < / b>');
map.setCenter(pos);
var marker = new google.maps.Marker({
position:pos,
map: map,
title:String(pos.lat)+,+ String(pos.lng),
});
},function(){
handleLocationError(true ,infoWindow,map.getCenter());
});
} else {
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());


解决方案

您可以使用反向地理编码这个目的。
下面是一个工作示例,其中提供给Google API的 latitude longitude



更多信息可以在这里找到。

https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse



  var geocoder = new google.maps.Geocoder; function geocodeLatLng(lat,lng){var latlng = {lat:lat,lng:lng}; geocoder.geocode({'location':latlng},function(results,status){if(status ==='OK'){if(results [0]){//这是你的格式化地址window.alert(results [0] .formatted_address);} else {window.alert('No results found');}} else {window.alert('Geocoder failed due to:'+ status);}});} geocodeLatLng(53.3478, - 6.2597);  

< script src =https:/ /maps.googleapis.com/maps/api/js?v=3&amp;sensor=false\"></script>


i have google map api which is able to get the current user location in latitude and longitude but instead i want to get the location name.

for example my location is bangalore => vidyaranyapura i want to get vidyaranyapura

here is demo : https://jsfiddle.net/aoe4vf17/100/

html:

<div id="map"></div>

javascript:

 var map = new google.maps.Map(document.getElementById("map"), {
   center: new google.maps.LatLng(53.3478, -6.2597),
   zoom: 16,
   mapTypeId: google.maps.MapTypeId.ROADMAP
 });

 var infoWindow = new google.maps.InfoWindow({
   map: map
 });
 // Try HTML5 geolocation.
 if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(function(position) {
     var pos = {
       lat: position.coords.latitude,
       lng: position.coords.longitude
     };
     console.log(position);
     infoWindow.setPosition(pos);
     infoWindow.setContent('<b>You are here.</b>');
     map.setCenter(pos);
     var marker = new google.maps.Marker({
       position: pos,
       map: map,
       title: String(pos.lat) + ", " + String(pos.lng),
     });
   }, function() {
     handleLocationError(true, infoWindow, map.getCenter());
   });
 } else {
   // Browser doesn't support Geolocation
   handleLocationError(false, infoWindow, map.getCenter());
 }

解决方案

You can use reverse geocoding for this purpose. Here is a working example with a latitude and longitude provided to the Google API.

More information can be found here.
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

var geocoder = new google.maps.Geocoder;

function geocodeLatLng(lat, lng) {

  var latlng = {
    lat: lat,
    lng: lng
  };

  geocoder.geocode({
    'location': latlng
  }, function(results, status) {
    if (status === 'OK') {
      if (results[0]) {

        //This is yout formatted address
        window.alert(results[0].formatted_address);

      } else {
        window.alert('No results found');
      }
    } else {
      window.alert('Geocoder failed due to: ' + status);
    }
  });

}


geocodeLatLng(53.3478, -6.2597);

<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>

这篇关于如何使用谷歌地图API获取当前位置名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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