1

答えのある同じ質問がたくさんあることは知っていますが、私のものを解決できないようです。

私が使用したコード:

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + results[0].geometry.location.lat() + "," + results[0].geometry.location.lng() + "&sensor=false&callback=?", function (data) {
    if (data[0]) {
        alert(data[0].formatted_address);
    }
});     

URLにアクセスするとjsonの結果を取得できますが、アプリケーション内では取得できません。削除する&callback=?と XmlHttpRequest エラーが発生しますが、削除するとタイトルのエラーが発生します。私が実際に取得している {json-content} の代わりに ({json-content}) のような形式のjsonを期待していると思われるいくつかの回答から読みました。もしそうなら、どうすれば正しいフォーマットを取得できますか?

編集

JSON 出力:

{
   "results" : [
     Uncaught SyntaxError: Unexpected token :
  {
     "address_components" : [
        {
           "long_name" : "81",
           "short_name" : "81",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Grange Rd",
           "short_name" : "Grange Rd",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "81 Grange Rd, Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "249586",
           "short_name" : "249586",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 249586",
     "geometry" : {
        "location" : {
           "lat" : 1.3029390,
           "lng" : 103.8230960
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304287980291502,
              "lng" : 103.8244449802915
           },
           "southwest" : {
              "lat" : 1.301590019708498,
              "lng" : 103.8217470197085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248649",
           "short_name" : "248649",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Orchard",
           "short_name" : "Orchard",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248649",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.30441750,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3030750,
           "lng" : 103.8242460
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304591980291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301894019708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "248643",
           "short_name" : "248643",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "River Valley",
           "short_name" : "River Valley",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore 248643",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.3032470,
              "lng" : 103.82452430
           },
           "southwest" : {
              "lat" : 1.30206850,
              "lng" : 103.82307520
           }
        },
        "location" : {
           "lat" : 1.3021720,
           "lng" : 103.8244280
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.304006730291502,
              "lng" : 103.8251487302915
           },
           "southwest" : {
              "lat" : 1.301308769708498,
              "lng" : 103.8224507697085
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Tanglin",
           "short_name" : "Tanglin",
           "types" : [ "sublocality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Tanglin, Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        },
        "location" : {
           "lat" : 1.29907550,
           "lng" : 103.8164260
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.32350930,
              "lng" : 103.83548650
           },
           "southwest" : {
              "lat" : 1.2919460,
              "lng" : 103.80224340
           }
        }
     },
     "types" : [ "sublocality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.3520830,
           "lng" : 103.8198360
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "country", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Singapore",
           "short_name" : "Singapore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Singapore",
           "short_name" : "SG",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Singapore",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        },
        "location" : {
           "lat" : 1.28943650,
           "lng" : 103.84998020
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 1.47077350,
              "lng" : 104.0843810
           },
           "southwest" : {
              "lat" : 1.19737070,
              "lng" : 103.61036090
           }
        }
     },
     "types" : [ "locality", "political" ]
  }
   ],
   "status" : "OK"
}

JSON リンク アドレス:

http://maps.googleapis.com/maps/api/geocode/json?latlng=1.302939,103.82309600000008&sensor=false&callback=jQuery17208982370237354189_1341999317106&_=1341999317754

4

1 に答える 1

2

JSONPこのAPIのサポートは削除されました-つまり、返されJSONないことを意味しJSONPます-そのため、エラーが発生します

ここでの重複した答えは、良い代替案またはこのようなものを提供します:

<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 latlong = new google.maps.LatLng("37.5097258429375","-122.27783203125");

geocoder.geocode({
    'latLng': latlong
}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].formatted_address);
    }
});​
</script>

ここでの実例

APIドキュメントはこちら

于 2012-07-11T10:03:01.553 に答える