2010-08-16 69 views
33

我在我的页面中有一个文本框,该文本框获取地址名称和带有文本getLat&Long的按钮。现在点击我的按钮,我必须在文本框中显示latitudelongitude的位置警报。任何建议?使用Google自动填充API获取基于地理位置名称的经度和纬度

+0

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+更换API KEY CA&sensor = true更多信息https://developers.google.com/maps/documentation/geocoding/ – zzart 2012-07-04 13:07:42

回答

37

您可以使用Google Maps API中的Google Geocoder service将您的位置名称转换为经度和纬度。所以,你需要一些这样的代码:

var geocoder = new google.maps.Geocoder(); 
var address = document.getElementById("address").value; 
geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) 
    { 
     // do something with the geocoded result 
     // 
     // results[0].geometry.location.latitude 
     // results[0].geometry.location.longitude 
    } 
}); 

更新

你包括V3的JavaScript API?

<script type="text/javascript" 
    src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false"> 
</script> 
+0

错误-'google.maps.Geocoder();'不是构造函数。 – Chandra 2010-08-16 07:00:21

+0

我如何使用它的来源和目的地我的意思是两个字段? – Wearybands 2014-04-10 16:30:13

+0

传感器的区别是真是假? – Serge 2015-11-03 10:20:12

8

下面是我使用的代码。它的工作完美。

var geo = new google.maps.Geocoder; 
geo.geocode({'address':address},function(results, status){ 
    if (status == google.maps.GeocoderStatus.OK) {    
     var myLatLng = results[0].geometry.location; 

     // Add some code to work with myLatLng    

    } else { 
     alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 

希望这会有所帮助。

2

我会建议下面的代码,你可以使用这个<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>来获取一个位置的经度和纬度,虽然它可能不是那么准确,但它为我工作;

下面

<!DOCTYPE html> 
<html> 
<head> 
<title>Using Javascript's Geolocation API</title> 

<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
</head> 
<body> 
<div id="mapContainer"></div> 

<script type="text/javascript"> 

     var lat = geoip_latitude(); 
     var long = geoip_longitude(); 
     document.write("Latitude: "+lat+"</br>Longitude: "+long); 

</script> 
</body> 
</html> 
31

的代码片段,我希望这可以帮助别人的未来。

您可以使用Google Geocoding API,如之前所说的,我不得不最近做一些这方面的工作,我希望这有助于:

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> 
     <script type="text/javascript"> 
     function initialize() { 
     var address = (document.getElementById('my-address')); 
     var autocomplete = new google.maps.places.Autocomplete(address); 
     autocomplete.setTypes(['geocode']); 
     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var place = autocomplete.getPlace(); 
      if (!place.geometry) { 
       return; 
      } 

     var address = ''; 
     if (place.address_components) { 
      address = [ 
       (place.address_components[0] && place.address_components[0].short_name || ''), 
       (place.address_components[1] && place.address_components[1].short_name || ''), 
       (place.address_components[2] && place.address_components[2].short_name || '') 
       ].join(' '); 
     } 
     }); 
} 
function codeAddress() { 
    geocoder = new google.maps.Geocoder(); 
    var address = document.getElementById("my-address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

     alert("Latitude: "+results[0].geometry.location.lat()); 
     alert("Longitude: "+results[0].geometry.location.lng()); 
     } 

     else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 
google.maps.event.addDomListener(window, 'load', initialize); 

     </script> 
    </head> 
    <body> 
     <input type="text" id="my-address"> 
     <button id="getCords" onClick="codeAddress();">getLat&Long</button> 
    </body> 
</html> 

现在这也是一个autocomlpete功能,你可以在代码中看到它会从输入中获取地址,并在输入时通过API自动完成。

一旦你有你的地址点击按钮,你会根据需要通过警报得到你的结果。另请注意,它使用最新的API并加载'places'库(当调用API使用'库'参数时)。

希望这有助于,并阅读文档以获取更多信息,欢呼声。

编辑#1Fiddle

+1

是的,谢谢! lat()lng()是完美的,因为它似乎从请求变为随机请求,具有诸如“k”和“A”之类的实际属性,或者像lat那样的其他奇怪的属性。你很聪明。 – 2014-03-11 21:31:38

+0

非常有帮助。你做了一个很棒的工作。感谢 – Randhir 2014-08-13 04:38:39

+0

这是否需要一个API密钥? – Supertecnoboff 2015-05-20 09:32:39

6

我希望这将是未来范围更加有用包含自动完成谷歌API的功能与 纬度和经度

var latitude = place.geometry.location.lat(); 
var longitude = place.geometry.location.lng(); 

完整视图

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Place Autocomplete With Latitude & Longitude </title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
#pac-input { 
    background-color: #fff; 
    padding: 0 11px 0 13px; 
    width: 400px; 
    font-family: Roboto; 
    font-size: 15px; 
    font-weight: 300; 
    text-overflow: ellipsis; 
} 
#pac-input:focus { 
    border-color: #4d90fe; 
    margin-left: -1px; 
    padding-left: 14px; /* Regular padding-left + 1. */ 
    width: 401px; 
} 
} 
</style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script> 
    <script> 


    function initialize() { 
     var address = (document.getElementById('pac-input')); 
     var autocomplete = new google.maps.places.Autocomplete(address); 
     autocomplete.setTypes(['geocode']); 
     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var place = autocomplete.getPlace(); 
      if (!place.geometry) { 
       return; 
      } 

     var address = ''; 
     if (place.address_components) { 
      address = [ 
       (place.address_components[0] && place.address_components[0].short_name || ''), 
       (place.address_components[1] && place.address_components[1].short_name || ''), 
       (place.address_components[2] && place.address_components[2].short_name || '') 
       ].join(' '); 
     } 
     /*********************************************************************/ 
     /* var address contain your autocomplete address *********************/ 
     /* place.geometry.location.lat() && place.geometry.location.lat() ****/ 
     /* will be used for current address latitude and longitude************/ 
     /*********************************************************************/ 
     document.getElementById('lat').innerHTML = place.geometry.location.lat(); 
     document.getElementById('long').innerHTML = place.geometry.location.lng(); 
     }); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
    </head> 
    <body> 
<input id="pac-input" class="controls" type="text" 
     placeholder="Enter a location"> 
<div id="lat"></div> 
<div id="long"></div> 
</body> 
</html> 
0

Ente通过自动完成的位置和所有字段的其余部分:纬度和Longititude值自动填充。
与谷歌API密钥

<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
<meta charset="utf-8"> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script> 

<link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"> 
</head> 

<body> 
<textarea placeholder="Enter Area name to populate Latitude and Longitude" name="address" onFocus="initializeAutocomplete()" id="locality" ></textarea><br> 

<input type="text" name="city" id="city" placeholder="City" value="" ><br> 
<input type="text" name="latitude" id="latitude" placeholder="Latitude" value="" ><br> 
<input type="text" name="longitude" id="longitude" placeholder="Longitude" value="" ><br> 
<input type="text" name="place_id" id="location_id" placeholder="Location Ids" value="" ><br> 

<script type="text/javascript"> 
    function initializeAutocomplete(){ 
    var input = document.getElementById('locality'); 
    // var options = { 
    // types: ['(regions)'], 
    // componentRestrictions: {country: "IN"} 
    // }; 
    var options = {} 

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

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     var place = autocomplete.getPlace(); 
     var lat = place.geometry.location.lat(); 
     var lng = place.geometry.location.lng(); 
     var placeId = place.place_id; 
     // to set city name, using the locality param 
     var componentForm = { 
     locality: 'short_name', 
     }; 
     for (var i = 0; i < place.address_components.length; i++) { 
     var addressType = place.address_components[i].types[0]; 
     if (componentForm[addressType]) { 
      var val = place.address_components[i][componentForm[addressType]]; 
      document.getElementById("city").value = val; 
     } 
     } 
     document.getElementById("latitude").value = lat; 
     document.getElementById("longitude").value = lng; 
     document.getElementById("location_id").value = placeId; 
    }); 
    } 
</script> 
</body> 
</html> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script src="//maps.googleapis.com/maps/api/js?libraries=places&key=API KEY"></script> 


<script src="https://fonts.googleapis.com/css?family=Roboto:300,400,500></script> 
相关问题