如何从json谷歌地图API中获取地理编码的经纬度? [英] How to get geocode latitude and longitude from json google map api in variables by javascript?
问题描述
JSON GEO位置
https:/ /maps.googleapis.com/maps/api/geocode/json?address=KOLKATA&key=API_KEY
{
results:[
{
address_components:[
{
long_name:Kolkata,
short_name:加尔各答,
types:[locality,political]
},
{
long_name:Kolkata,
short_name :加尔各答,
类型:[administrative_area_level_2,political]
},
{
long_name:西孟加拉邦,
short_name:WB,
types:[administrative_area_level_1,political]
},
{
long_name:印度,
short_name:IN,
types:[country,political]
}
formatted_address:印度西孟加拉邦加尔各答,
geometry:{
bounds:{
northeast:{
lat:23.0078201,
lng:88.5428696
},
southwest:{
lat:22.3436288,
lng:88.19430439999999
$位置$ {
lat:22.572646,
lng:88.36389500000001
},
location_type:APPROXIMATE,
viewport:{
northeast:{
lat:23.0078201,
lng:88.5428696
},
southwest:{
lat:22.3436288,
lng:88.19430439999999 $
place_id:ChIJZ_YISduC-DkRvCxsj-Yw40M,
types:[locality,political] $ b b
$ b $
$ status
$ b JS
$(function(){
$(#home_city_select)。change (){
// alert($('option:selected',this).text());
var city = document.getElementById(home_city_select)。value;
var ltd;
var lng;
var jsonLtdLng =https://maps.googleapis.com/maps/api/geocode/json?address=+ city +& key = API_KEY;
//alert(JSON.stringify(jsonLtdLng));
//alert($.getJSON(jsonLtdLng));
//alert($.getJSON(jsonLtdLng.lat));
//alert($.getJSON(jsonLtdLng.results.geometry.location.lat));
//alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));
// alert($。getJSON(jsonLtdLng.results [0] .geometry.location.lat()));
alert(jsonLtdLng.results [0] .geometry.location.lat());
});
});
在IBM Worklight中进行测试。
但我无法获得经纬度$ / b
$ b
警报(JSON.stringify(jsonLtdLng)); //返回完整地址
< ($。getJSON(jsonLtdLng.lat)); //返回任何对象
alert($。getJSON(jsonLtdLng.lat)); >
alert($。getJSON(jsonLtdLng.results.geometry.location.lat)); //不返回任何内容
($。getJSON(jsonLtdLng.results [0] .geometry.location.lat)); //不返回任何内容
alert($。getJSON(jsonLtdLng.results [ 0] .geometry.location.lat())); //什么都不返回
alert(jsonLtdLng.results [0] .geometry.location.lat()); //返回任何内容
解决方案您对 $。getJSON
并不完全正确试试这个:
$。getJSON(jsonLtdLng,function(data){
的console.log(data.results [0] .geometry.location.lat);
});
JSON GEO LOCATION
https://maps.googleapis.com/maps/api/geocode/json?address=KOLKATA&key=API_KEY
{
"results" : [
{
"address_components" : [
{
"long_name" : "Kolkata",
"short_name" : "Kolkata",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kolkata",
"short_name" : "Kolkata",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "West Bengal",
"short_name" : "WB",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Kolkata, West Bengal, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 23.0078201,
"lng" : 88.5428696
},
"southwest" : {
"lat" : 22.3436288,
"lng" : 88.19430439999999
}
},
"location" : {
"lat" : 22.572646,
"lng" : 88.36389500000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 23.0078201,
"lng" : 88.5428696
},
"southwest" : {
"lat" : 22.3436288,
"lng" : 88.19430439999999
}
}
},
"place_id" : "ChIJZ_YISduC-DkRvCxsj-Yw40M",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
JS
$(function() {
$("#home_city_select").change(function() {
//alert( $('option:selected', this).text() );
var city = document.getElementById("home_city_select").value;
var ltd;
var lng;
var jsonLtdLng="https://maps.googleapis.com/maps/api/geocode/json?address="+city+"&key=API_KEY";
//alert(JSON.stringify(jsonLtdLng));
//alert($.getJSON(jsonLtdLng));
//alert($.getJSON(jsonLtdLng.lat));
//alert($.getJSON(jsonLtdLng.results.geometry.location.lat));
//alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));
//alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));
alert(jsonLtdLng.results[0].geometry.location.lat());
});
});
Testing in IBM Worklight.
But I cannot get the latitude and longitude
alert(JSON.stringify(jsonLtdLng));//returns full address
alert($.getJSON(jsonLtdLng));//returns object Object
alert($.getJSON(jsonLtdLng.lat));//returns nothing
alert($.getJSON(jsonLtdLng.results.geometry.location.lat));//returns nothing
alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));//returns nothing
alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));//returns nothing
alert(jsonLtdLng.results[0].geometry.location.lat());//returns nothing
解决方案 Your call to $.getJSON
was not totally correct
Try this :
$.getJSON(jsonLtdLng, function (data) {
console.log(data.results[0].geometry.location.lat);
});
这篇关于如何从json谷歌地图API中获取地理编码的经纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!