简体   繁体   English

如何通过JavaScript从JSON Google Map API中获取地理编码纬度和经度?

[英]How to get geocode latitude and longitude from json google map api in variables by javascript?

JSON GEO LOCATION https://maps.googleapis.com/maps/api/geocode/json?address=KOLKATA&key=API_KEY JSON GEO位置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 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. 在IBM Worklight中进行测试。

But I cannot get the latitude and longitude 但是我无法获得经度和纬度

alert(JSON.stringify(jsonLtdLng));//returns full address alert(JSON.stringify(jsonLtdLng)); //返回完整地址

alert($.getJSON(jsonLtdLng));//returns object Object alert($。getJSON(jsonLtdLng)); //返回对象Object

alert($.getJSON(jsonLtdLng.lat));//returns nothing alert($。getJSON(jsonLtdLng.lat)); //不返回任何内容

alert($.getJSON(jsonLtdLng.results.geometry.location.lat));//returns nothing alert($。getJSON(jsonLtdLng.results.geometry.location.lat)); //不返回任何内容

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));//returns nothing alert($。getJSON(jsonLtdLng.results [0] .geometry.location.lat)); //不返回任何内容

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));//returns nothing alert($。getJSON(jsonLtdLng.results [0] .geometry.location.lat())); //不返回任何内容

alert(jsonLtdLng.results[0].geometry.location.lat());//returns nothing alert(jsonLtdLng.results [0] .geometry.location.lat()); //不返回任何内容

Your call to $.getJSON was not totally correct 您对$.getJSON调用并不完全正确

Try this : 尝试这个 :

$.getJSON(jsonLtdLng, function (data) {
  console.log(data.results[0].geometry.location.lat);
});

I think you can use console.log() , alert() or document.write() as long as the information is deserialized, I am using JSON.parse() for deserializing, eval() is what people used before. 我认为您可以使用console.log()alert()document.write() ,只要对信息进行反序列化,我就使用JSON.parse()进行反序列化,而eval()是人们以前使用的东西。 Deserializing means to recover the data encoded in json so Javascript can use it as an object. 反序列化意味着恢复以json编码的数据,因此Javascript可以将其用作对象。 In fact, if you use write.document(someJson) you will see [object object] meaning you have all your data there ready to be used, but you need to JSON.parse() it first. 实际上,如果使用write.document(someJson),您将看到[object object]意味着您已准备好使用所有数据,但首先需要对其进行JSON.parse() JSON.stringnify() is the opposite of what you want to do, because that function is for serialize the data and then transmit it, then the receptor needs to use JSON.parse() to deserialized it and use it in Javascript. JSON.stringnify()与您要执行的操作相反,因为该函数用于序列化数据然后传输它,然后接收器需要使用JSON.parse()反序列化它并在Javascript中使用它。 I like to use write.document() in order to see the information, but console log allows you to see what is inside of the object easier. 我喜欢使用write.document()来查看信息,但是控制台日志使您可以更轻松地查看对象内部的内容。 Here, some code example: 下面是一些代码示例:

// apiData would be the variable with the API information
var externalData = JSON.parse(apiData);

// will output [object object]     
document.write(externalData);

// will output  “     “
document.write('      ');

// will output the latitude
document.write(externalData.results[0].geometry.location.lat);

// will output in the console, you need to type externalData in the console log 
console.log(externalData);

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JavaScript和Google的地理编码API通过纬度和经度放置标记 - Placing markers via latitude and longitude with JavaScript and Google's geocode API 从Javascript / Geocoder.geocode()获取纬度和经度,并将其与php一起使用 - Get Latitude and Longitude From Javascript/Geocoder.geocode() and using it with php 需要从谷歌地图 API 获取纬度和经度 - Need to get latitude and longitude from google map API 将经度和纬度传递给javascript函数,并从Google地图获取视图 - Pass Latitude and Longitude to a javascript function and get a view from the google map 如何使用google map api在php中获取当前的纬度和经度? - How to get current latitude and longitude in php using google map api? 如何获取google maps api v3 javascript中的地图的纬度,经度onclick? - how to get latitude, longitude onclick of a map in google maps api v3 javascript? Google Maps API - 如何在不显示地图的情况下从自动完成获取纬度和经度? - Google Maps API - how to get latitude and longitude from Autocomplete without showing the map? 如何从Google地方地图中获取经度和纬度? - How to get latitude and longitude from google place map? 如何从Google地图获取当前的经度和纬度 - How to get current latitude and longitude from Google map 如何从html表单到谷歌地图获取纬度经度值 - How to get latitude longitude value from html form to google map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM