Javascript geocoding from address to latitude and longitude numbers not working

前端 未结 3 1397
故里飘歌
故里飘歌 2020-12-04 17:41

I\'m using the following geocoding function to convert a textual address into latitude and longitude numbers, but it\'s not working right. The alert writes \"undefined\".

3条回答
  •  执笔经年
    2020-12-04 18:18

    The script tag to the api has changed recently. Use something like this to query the Geocoding API and get the JSON object back

    
    

    The address could be something like

    1600+Amphitheatre+Parkway,+Mountain+View,+CA (URI Encoded; you should Google it. Very useful)

    or simply

    1600 Amphitheatre Parkway, Mountain View, CA

    By entering this address https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY inside the browser, along with my API Key, I get back a JSON object which contains the Latitude & Longitude for the city of Moutain view, CA.

    {"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"}
    

    Web Frameworks such like AngularJS allow us to perform these queries with ease.

提交回复
热议问题