how to parse a Google Maps geocoding result

后端 未结 2 717
长情又很酷
长情又很酷 2021-01-14 07:57

I want to retrieve Geo address from given latitude and longitude using geocoding, using the given example on the site

http://maps.googleapis.com/maps/api/geocod

2条回答
  •  攒了一身酷
    2021-01-14 08:39

    I have a simple solution that works for the Google Geocode JSON results. It is a simple for loop that returns that short and long version in a string array. Just call the function and provide the "types" in the argument.

    string[] street_number = GetJSONValueBasedOnType(record.geocode, "street_number");

        public string[] GetJSONValueBasedOnType(NewGoogleGeocode gc, string WhatYouWant)
    {
        string[] returns = new string[2];
        returns[0] = string.Empty;
        returns[1] = string.Empty;
        bool isInIt;
    
        foreach (var v in gc.results[0].address_components)
        {
            isInIt = false;
            foreach (var t in v.types)
            {
                if (t == WhatYouWant)
                {
                    isInIt = true;
                }
            }
            if (isInIt)
            {
                returns[0] = v.short_name;
                returns[1] = v.long_name;
            }
        }
        return returns;
    }
    

提交回复
热议问题