Ash*_*Ash 56 javascript google-maps
我正在使用以下地理编码功能将文本地址转换为纬度和经度数字,但它无法正常工作.警报写入"未定义".
谁能说出错了?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.latitude;
var longitude = results[0].geometry.location.longitude;
alert(latitude);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
Ska*_*lig 86
请尝试使用此代码:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
Run Code Online (Sandbox Code Playgroud)
导航谷歌的api有点难,但这里是相关的文档.
我找不到的一件事是如何走向另一个方向.从坐标到地址.这是我使用upp的代码.请注意,我也使用jquery.
$.each(results[0].address_components, function(){
$("#CreateDialog").find('input[name="'+ this.types+'"]').attr('value', this.long_name);
});
Run Code Online (Sandbox Code Playgroud)
我正在做的是遍历所有返回address_components
并测试它们的类型是否与表单中的任何输入元素名称匹配.如果他们这样做,我将元素的address_components
值设置为值.
如果您只对整个格式化地址感兴趣,那么您可以关注Google的示例
Dan*_*umb 46
您错误地访问了纬度和经度.
尝试
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
api 的脚本标签最近发生了变化。使用类似的方法来查询 Geocoding API 并获取 JSON 对象
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/geocode/json?address=THE_ADDRESS_YOU_WANT_TO_GEOCODE&key=YOUR_API_KEY"></script>
Run Code Online (Sandbox Code Playgroud)
地址可能类似于
1600+露天剧场+公园路,+山+景观,+CA(URI编码;你应该谷歌它。非常有用)
或者简单地
1600 Amphitheatre Parkway, 山景城, CA
通过在浏览器中输入此地址https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
以及我的API 密钥,我会得到一个 JSON 对象,其中包含加利福尼亚州山景市的纬度和经度。
{"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4222556,
"lng" : -122.0838589
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4236045802915,
"lng" : -122.0825099197085
},
"southwest" : {
"lat" : 37.4209066197085,
"lng" : -122.0852078802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}],"status" : "OK"}
Run Code Online (Sandbox Code Playgroud)
像AngularJS这样的 Web 框架允许我们轻松执行这些查询。