簡體   English   中英

您如何解析此Google Maps JSON

[英]how do you parse this google maps JSON

我通過以下方式使用Google地方信息:

https://ubilabs.github.io/geocomplete/

var geocomp =  $("#geocomplete").geocomplete({types:['establishment', 'geocode']}); 

我得到以下JSON:

{
    "address_components": [
        {
            "long_name": "Empire State Building",
            "short_name": "Empire State Building",
            "types": [
                "point_of_interest",
                "establishment"
            ]
        },
        {

        },
        …{

        }
    ],
    "formatted_address": "Empire State Building, 350 5th Ave, New York, NY 10118, USA",
    "geometry": {
        "location": {
            "lat": 40.7484405,
            "lng": -73.98566440000002
        },
        "location_type": "APPROXIMATE",
        "viewport": {
            "south": 40.7470915197085,
            "west": -73.9870133802915,
            "north": 40.7497894802915,
            "east": -73.98431541970848
        }
    },
    "place_id": "ChIJaXQRs6lZwokRY6EFpJnhNNE",
    "types": [
        "point_of_interest",
        "establishment"
    ]
}

它在以下回調中返回JSON數據:

$("#geocomplete")
  .bind("geocode:result", function(event, result){
}

我能夠按以下方式訪問address_components數組的元素:

result.address_components[0].long_name
result.address_components[1].long_name
result.address_components[2].long_name

如何在location.geometry中訪問“ lat”和“ lng”?

我試過了

result.geometry.location.lat        
result.geometry.location.lng

並說lat包含:

function (){return a}

並且lng包含:

function (){return b}

這對我來說沒有意義,因為開頭給出了JSON。

如何訪問這兩個值?

我認為locationgoogle.maps.LatLng對象。 無論如何,您都可以使用result.geometry.location.lat()result.geometry.location.lng()訪問坐標。

這是您需要的:我的示例演示了如何獲取特定地址的數據,但我認為它將起作用。

類別:

 public class GoogleGeoCodeResponse
    {

        public string status { get; set; }
        public results[] results { get; set; }
        public string error_message { get; set; }
        public string next_page_token { get; set; }

    }

    public class results
    {
        public string name { get; set; }
        public string formatted_address { get; set; }
        public geometry geometry { get; set; }
        public string[] types { get; set; }
        public address_component[] address_components { get; set; }
        public string vicinity { get; set; }
        public string place_id { get; set; }
        public string international_phone_number { get; set; }
    }

    public class geometry
    {
        public string location_type { get; set; }
        public location location { get; set; }
    }

    public class location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class GooglePalceDetail
    {
        public results result { get; set; }
        public string status { get; set; }
        public string error_message { get; set; }

    }

    public class address_component
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public string[] types { get; set; }
    }

控制器:

var address = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}", currentAddress, googleKey);

 string json = client.DownloadString(address);
 var gglGeo = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);

if ((gglGeo.status == "ZERO_RESULTS")){
 ViewBag.msg="no results";
 return View()
}
 var lat = gglGeo.results[0].geometry.location.lat;
 var lng = gglGeo.results[0].geometry.location.lng;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM