Json对象在控制台中返回错误的结果

时间:2018-03-22 05:28:20

标签: javascript jquery json google-maps

我需要获取full address以及latlong值,请使用以下json



var place = {
  "address_components": [{
      "long_name": "17",
      "short_name": "17",
      "types": [
        "street_number"
      ]
    },
    {
      "long_name": "Via Gavino Alivia",
      "short_name": "Via Gavino Alivia",
      "types": [
        "route"
      ]
    },
    {
      "long_name": "Sassari",
      "short_name": "Sassari",
      "types": [
        "locality",
        "political"
      ]
    },
    {
      "long_name": "Sassari",
      "short_name": "Sassari",
      "types": [
        "administrative_area_level_3",
        "political"
      ]
    },
    {
      "long_name": "Provincia di Sassari",
      "short_name": "SS",
      "types": [
        "administrative_area_level_2",
        "political"
      ]
    },
    {
      "long_name": "Sardegna",
      "short_name": "Sardegna",
      "types": [
        "administrative_area_level_1",
        "political"
      ]
    },
    {
      "long_name": "Italia",
      "short_name": "IT",
      "types": [
        "country",
        "political"
      ]
    },
    {
      "long_name": "07100",
      "short_name": "07100",
      "types": [
        "postal_code"
      ]
    }
  ],
  "adr_address": "<span class=\"street-address\">Via Gavino Alivia, 17</span>, <span class=\"postal-code\">07100</span> <span class=\"locality\">Sassari</span> <span class=\"region\">SS</span>, <span class=\"country-name\">Italia</span>",
  "formatted_address": "Via Gavino Alivia, 17, 07100 Sassari SS, Italia",
  "geometry": {
    "location": {
      "lat": 40.7272074,
      "lng": 8.575266499999998
    },
    "viewport": {
      "south": 40.7258300197085,
      "west": 8.573940919708548,
      "north": 40.7285279802915,
      "east": 8.576638880291512
    }
  },
  "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
  "id": "60b613592143b21a673925a803f3a37e74692be2",
  "name": "Via Gavino Alivia, 17",
  "place_id": "ChIJ-_S-5gZk3BIRnEAF-pcOiGc",
  "reference": "CmRbAAAAak30NgQLPA1jRYRq1gaOvw2nvBaLEb7vDYYWW-8txVZE9PPm5NfqR92aVMDrz-_hpsfL3COT-5fC3jEmuWUuanEJLGp4fQAYjVeQ3KOV-jKVY1WaE1sqIzLyq_qzrrSmEhDwhG5nCw7umUscO6SunN1LGhTYgXhJnFle42TgdiUMjJV48MMl4w",
  "scope": "GOOGLE",
  "types": [
    "street_address"
  ],
  "url": "https://maps.google.com/?q=Via+Gavino+Alivia,+17,+07100+Sassari+SS,+Italia&ftid=0x12dc6406e6bef4fb:0x67880e97fa05409c",
  "utc_offset": 60,
  "vicinity": "Sassari",
  "html_attributions": []
};

console.log(place.geometry.location.lat);
console.log(place.geometry.location.lng);
console.log(place.formatted_address);
&#13;
&#13;
&#13; 如果我执行以下操作来获取latlng

console.log(place.geometry.location.lat);
console.log(place.geometry.location.lng);

我明白了:

ƒ (){return a}
ƒ (){return b}

如果我对完整地址做同样的事情就好了:

console.log(place.formatted_address);

我得到:

Via Gavino Alivia, 17, 07100 Sassari SS, Italia

我做错了什么?

更新

完整代码:

new L.Control.GPlaceAutocomplete({
  callback: function(place){
    $(".leaflet-marker-icon, .leaflet-shadow-pane").remove();
    var loc = place.geometry.location;
    map.setView( [loc.lat(), loc.lng()], 4);
    new L.marker([loc.lat(), loc.lng()]).addTo(map);
    console.log(place.formatted_address);
    console.log(place.geometry.location.lat);
    console.log(place.geometry.location.lng);
  }
}).addTo(map);

控制台:

Via Gavino Alivia, 17, 07100 Sassari SS, Italia
ƒ (){return a}
ƒ (){return b}

2 个答案:

答案 0 :(得分:0)

使用它来获取lat和lng:

place.geometry.location.lat() place.geometry.location.lng()

答案 1 :(得分:0)

工作正常。请在下面的代码中查看。

&#13;
&#13;
var place = {
    "address_components": [
        {
            "long_name": "17",
            "short_name": "17",
            "types": [
                "street_number"
            ]
        },
        {
            "long_name": "Via Gavino Alivia",
            "short_name": "Via Gavino Alivia",
            "types": [
                "route"
            ]
        },
        {
            "long_name": "Sassari",
            "short_name": "Sassari",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "Sassari",
            "short_name": "Sassari",
            "types": [
                "administrative_area_level_3",
                "political"
            ]
        },
        {
            "long_name": "Provincia di Sassari",
            "short_name": "SS",
            "types": [
                "administrative_area_level_2",
                "political"
            ]
        },
        {
            "long_name": "Sardegna",
            "short_name": "Sardegna",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "Italia",
            "short_name": "IT",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "07100",
            "short_name": "07100",
            "types": [
                "postal_code"
            ]
        }
    ],
    "adr_address": "<span class=\"street-address\">Via Gavino Alivia, 17</span>, <span class=\"postal-code\">07100</span> <span class=\"locality\">Sassari</span> <span class=\"region\">SS</span>, <span class=\"country-name\">Italia</span>",
    "formatted_address": "Via Gavino Alivia, 17, 07100 Sassari SS, Italia",
    "geometry": {
        "location": {
            "lat": 40.7272074,
            "lng": 8.575266499999998
        },
        "viewport": {
            "south": 40.7258300197085,
            "west": 8.573940919708548,
            "north": 40.7285279802915,
            "east": 8.576638880291512
        }
    },
    "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
    "id": "60b613592143b21a673925a803f3a37e74692be2",
    "name": "Via Gavino Alivia, 17",
    "place_id": "ChIJ-_S-5gZk3BIRnEAF-pcOiGc",
    "reference": "CmRbAAAAak30NgQLPA1jRYRq1gaOvw2nvBaLEb7vDYYWW-8txVZE9PPm5NfqR92aVMDrz-_hpsfL3COT-5fC3jEmuWUuanEJLGp4fQAYjVeQ3KOV-jKVY1WaE1sqIzLyq_qzrrSmEhDwhG5nCw7umUscO6SunN1LGhTYgXhJnFle42TgdiUMjJV48MMl4w",
    "scope": "GOOGLE",
    "types": [
        "street_address"
    ],
    "url": "https://maps.google.com/?q=Via+Gavino+Alivia,+17,+07100+Sassari+SS,+Italia&ftid=0x12dc6406e6bef4fb:0x67880e97fa05409c",
    "utc_offset": 60,
    "vicinity": "Sassari",
    "html_attributions": []
};

console.log("lat", place.geometry.location.lat);
console.log("lng", place.geometry.location.lng);
console.log("address", place.formatted_address);
&#13;
&#13;
&#13;