我在我的页面中有一个文本框,该文本框获取地址名称和带有文本getLat&Long
的按钮。现在点击我的按钮,我必须在文本框中显示latitude
和longitude
的位置警报。任何建议?使用Google自动填充API获取基于地理位置名称的经度和纬度
回答
您可以使用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>
错误-'google.maps.Geocoder();'不是构造函数。 – Chandra 2010-08-16 07:00:21
我如何使用它的来源和目的地我的意思是两个字段? – Wearybands 2014-04-10 16:30:13
传感器的区别是真是假? – Serge 2015-11-03 10:20:12
http://maps.googleapis.com/maps/api/geocode/OUTPUT?address=YOUR_LOCATION&sensor=true
OUTPUT = JSON或XML;
关于谷歌地图API的详细信息,请通过网址:
http://code.google.com/apis/maps/documentation/geocoding/index.html
希望这将有助于
这太棒了!只有一个问题,这是否需要API密钥? – Supertecnoboff 2015-05-20 09:32:23
下面是我使用的代码。它的工作完美。
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);
}
});
希望这会有所帮助。
我会建议下面的代码,你可以使用这个<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>
的代码片段,我希望这可以帮助别人的未来。
您可以使用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使用'库'参数时)。
希望这有助于,并阅读文档以获取更多信息,欢呼声。
编辑#1:Fiddle
是的,谢谢! lat()lng()是完美的,因为它似乎从请求变为随机请求,具有诸如“k”和“A”之类的实际属性,或者像lat那样的其他奇怪的属性。你很聪明。 – 2014-03-11 21:31:38
非常有帮助。你做了一个很棒的工作。感谢 – Randhir 2014-08-13 04:38:39
这是否需要一个API密钥? – Supertecnoboff 2015-05-20 09:32:39
我希望这将是未来范围更加有用包含自动完成谷歌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>
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>
- 1. 获取基于给定名称的经度和纬度的API
- 2. 从Google地理定位API获取当前经纬度和lng
- 3. 从不使用Google API的地址获取经度和纬度
- 4. 在BingMap中获取经度和纬度的位置名称
- 5. 使用纬度和经度的位置名称的Web服务
- 6. 如何使用HTML5地理定位或Google API获取地址经纬度?
- 7. 如何通过使用纬度和经度值获取位置名称
- 8. 如何使用纬度和经度获取位置名称或城市?
- 9. 从php的经度和纬度获取地区名称
- 10. 从经纬度获取位置地址
- 11. 获取用户位置的纬度经度与API 22
- 12. 谷歌地图API返回地方'基于经度和纬度
- 13. 获取基于城市,邮编或街道名称的经度和纬度
- 14. 获取地标的经度和纬度值 - Google地球
- 15. 如何获取与Google地图相同的Google Geocoder API几何值(经度和纬度)URL经纬度值
- 16. 显示位置使用经度/纬度坐标 - Google地图
- 17. 谷歌地图api - 如何从纬度/经度获取地方的名称
- 18. 获取基于距离和度数的经度纬度组合
- 19. 在Google Places API中嵌入当前位置(经度和纬度)
- 20. 如何通过编程或使用api获取位置的纬度和经度
- 21. 获取纬度和经度
- 22. 获取经度和纬度
- 23. 使用javascript获取纬度和经度
- 24. 基于经度和纬度计算最近位置的算法
- 25. 谷歌地图API(附近获得纬度经度位置)
- 26. 使用API查找已知位置的经度和纬度?
- 27. Google Maps地理编码API - 处理来自用户的纬度/经度输入
- 28. 从城市名称获取经纬度
- 29. 从经纬度获取城市名称
- 30. 获取当前位置的经纬度
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