从Google Map中的Geocode获取North,South,East,West街道名称

时间:2015-06-04 16:18:21

标签: javascript c# google-maps google-maps-api-3

我已经通过谷歌地图实现了属性搜索,并且每个属性都将在谷歌地图中标记为一个引脚。在我需要的时候,用户可以知道北,南,东,西,东北,西北,西南和东南方向的单一属性(地理编码)的街道名称。我只需要列出每个方向的街道名称。

例如,我正在搜索以下属性,它必须返回

财产:18,Maha street,Coimbatore,Tamilnadu (27.268255,-95.55666)

North : Main street
South : Anna street
East : River road
West : School street
NorthEast : KMSkj Street
NorthWest : Ring road
SouthWest : Hasbdagh Road
SouthEast : Outer ring road

这可以在谷歌地图中实现吗?

如果我获得所有方向的地理编码,那么我们可以正确应用反向地理编码吗?

1 个答案:

答案 0 :(得分:0)

我通过服务器端地理编码过程实现了这一点,下面是涉及的方法:

    public GeocoderLocation Locate(string query)
    {
        GeocoderLocation N = new GeocoderLocation();
        GeocoderLocation S = new GeocoderLocation();
        GeocoderLocation E = new GeocoderLocation();
        GeocoderLocation W = new GeocoderLocation();
        GeocoderLocation NE = new GeocoderLocation();
        GeocoderLocation NW = new GeocoderLocation();
        GeocoderLocation SE = new GeocoderLocation();
        GeocoderLocation SW = new GeocoderLocation();

        WebRequest request = WebRequest
           .Create("http://maps.google.com/maps/api/geocode/xml?sensor=false&size=750x700&address="
              + HttpUtility.UrlEncode(query));

        using (WebResponse response = request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                XDocument document = XDocument.Load(new StreamReader(stream));

                List<XElement> longitudeElement = document.Descendants("lng").ToList();
                List<XElement> latitudeElement = document.Descendants("lat").ToList();

                if (latitudeElement != null && latitudeElement.Count == 3 && longitudeElement != null && longitudeElement.Count == 3)
                {
                    NE.Longitude = Double.Parse(longitudeElement[1].Value, CultureInfo.InvariantCulture);
                    NE.Latitude = Double.Parse(latitudeElement[1].Value, CultureInfo.InvariantCulture);

                    SW.Longitude = Double.Parse(longitudeElement[2].Value, CultureInfo.InvariantCulture);
                    SW.Latitude = Double.Parse(latitudeElement[2].Value, CultureInfo.InvariantCulture);

                    NW.Latitude = NE.Latitude;
                    NW.Longitude = SW.Longitude;

                    SE.Latitude = SW.Latitude;
                    SE.Longitude = NE.Longitude;
                }
                List<GeocoderLocation> NList = new List<GeocoderLocation>();
                NList.Add(NE);
                NList.Add(NW);
                N = GetCentralGeoCoordinate(NList);

                List<GeocoderLocation> SList = new List<GeocoderLocation>();
                SList.Add(SE);
                SList.Add(SW);
                S = GetCentralGeoCoordinate(SList);

                List<GeocoderLocation> EList = new List<GeocoderLocation>();
                EList.Add(SE);
                EList.Add(NE);
                E = GetCentralGeoCoordinate(EList);

                List<GeocoderLocation> WList = new List<GeocoderLocation>();
                WList.Add(NW);
                WList.Add(SW);
                W = GetCentralGeoCoordinate(WList);

                string Address_ShortName = string.Empty;
                string Address_country = string.Empty;
                string Address_administrative_area_level_1 = string.Empty;
                string Address_administrative_area_level_2 = string.Empty;
                string Address_administrative_area_level_3 = string.Empty;
                string Address_colloquial_area = string.Empty;
                string Address_locality = string.Empty;
                string Address_sublocality = string.Empty;
                string Address_neighborhood = string.Empty;
                string Address_streetname1 = string.Empty;
                string Address_streetname2 = string.Empty;
                string Address_streetname3 = string.Empty;
                string Address_streetname4 = string.Empty;
                string Address_streetname5 = string.Empty;
                string Address_streetname6 = string.Empty;
                string Address_streetname7 = string.Empty;
                string Address_streetname8 = string.Empty;

                if (N != null)
                {
                    string str = ReverseGeoLoc(N.Longitude.ToString(), N.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood,out Address_streetname1);

                    txtNorth.Text = Address_streetname1;
                }

                if (W != null)
                {
                    string str = ReverseGeoLoc(W.Longitude.ToString(), W.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood, out Address_streetname2);

                    txtWest.Text = Address_streetname2;
                }

                if (S != null)
                {
                    string str = ReverseGeoLoc(S.Longitude.ToString(), S.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood,out Address_streetname3);

                    txtSouth.Text = Address_streetname3;
                }

                if (NW != null)
                {
                    string str = ReverseGeoLoc(NW.Longitude.ToString(), NW.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood, out Address_streetname4);

                    txtNorthWest.Text = Address_streetname4;
                }

                if (NE != null)
                {
                    string str = ReverseGeoLoc(NE.Longitude.ToString(), NE.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood, out Address_streetname5);

                    txtNorthEast.Text = Address_streetname5;
                }

                if (SE != null)
                {
                    string str = ReverseGeoLoc(SE.Longitude.ToString(), SE.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood,out Address_streetname6);

                    txtSouthEast.Text = Address_streetname6;
                }

                if (SW != null)
                {
                    string str = ReverseGeoLoc(SW.Longitude.ToString(), SW.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood, out Address_streetname7);

                    txtSouthWest.Text = Address_streetname7;
                }

                if (E != null)
                {
                    string str = ReverseGeoLoc(E.Longitude.ToString(), E.Latitude.ToString(), out Address_ShortName,
                        out Address_country, out Address_administrative_area_level_1,
                        out Address_administrative_area_level_2, out Address_administrative_area_level_3,
                        out Address_colloquial_area, out Address_locality, out Address_sublocality,
                        out  Address_neighborhood, out Address_streetname8);

                    txtEast.Text = Address_streetname8;
                }

            }

        }

        return null;
    }

    public GeocoderLocation GetCentralGeoCoordinate(IList<GeocoderLocation> geoCoordinates)
    {
        if (geoCoordinates.Count == 1)
        {
            return geoCoordinates.Single();
        }

        double x = 0;
        double y = 0;
        double z = 0;

        foreach (var geoCoordinate in geoCoordinates)
        {
            var latitude = geoCoordinate.Latitude * Math.PI / 180;
            var longitude = geoCoordinate.Longitude * Math.PI / 180;

            x += Math.Cos(latitude) * Math.Cos(longitude);
            y += Math.Cos(latitude) * Math.Sin(longitude);
            z += Math.Sin(latitude);
        }

        var total = geoCoordinates.Count;

        x = x / total;
        y = y / total;
        z = z / total;

        var centralLongitude = Math.Atan2(y, x);
        var centralSquareRoot = Math.Sqrt(x * x + y * y);
        var centralLatitude = Math.Atan2(z, centralSquareRoot);

        return new GeocoderLocation
        {
            Latitude = centralLatitude * 180 / Math.PI,
            Longitude = centralLongitude * 180 / Math.PI

        };
    }

    public static string ReverseGeoLoc(string longitude, string latitude,
    out string Address_ShortName,
    out string Address_country,
    out string Address_administrative_area_level_1,
    out string Address_administrative_area_level_2,
    out string Address_administrative_area_level_3,
    out string Address_colloquial_area,
    out string Address_locality,
    out string Address_sublocality,
    out string Address_neighborhood,
     out string Address_streetname)
    {

        Address_ShortName = "";
        Address_country = "";
        Address_administrative_area_level_1 = "";
        Address_administrative_area_level_2 = "";
        Address_administrative_area_level_3 = "";
        Address_colloquial_area = "";
        Address_locality = "";
        Address_sublocality = "";
        Address_neighborhood = "";
        Address_streetname = "";
        XmlDocument doc = new XmlDocument();

        try
        {
            doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&sensor=false");
            XmlNode element = doc.SelectSingleNode("//GeocodeResponse/status");
            if (element.InnerText == "ZERO_RESULTS")
            {
                return ("No data available for the specified location");
            }
            else
            {

                element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");

                string longname = "";
                string shortname = "";
                string typename = "";
                bool fHit = false;


                XmlNodeList xnList = doc.SelectNodes("//GeocodeResponse/result/address_component");
                foreach (XmlNode xn in xnList)
                {
                    try
                    {
                        longname = xn["long_name"].InnerText;
                        shortname = xn["short_name"].InnerText;
                        typename = xn["type"].InnerText;


                        fHit = true;
                        switch (typename)
                        {
                            //Add whatever you are looking for below
                            case "country":
                                {
                                    Address_country = longname;
                                    Address_ShortName = shortname;
                                    break;
                                }

                            case "locality":
                                {
                                    Address_locality = longname;
                                    //Address_locality = shortname; //Om Longname visar sig innehålla konstigheter kan man använda shortname istället
                                    break;
                                }

                            case "sublocality":
                                {
                                    Address_sublocality = longname;
                                    break;
                                }

                            case "neighborhood":
                                {
                                    Address_neighborhood = longname;
                                    break;
                                }

                            case "colloquial_area":
                                {
                                    Address_colloquial_area = longname;
                                    break;
                                }

                            case "administrative_area_level_1":
                                {
                                    Address_administrative_area_level_1 = longname;
                                    break;
                                }

                            case "administrative_area_level_2":
                                {
                                    Address_administrative_area_level_2 = longname;
                                    break;
                                }

                            case "administrative_area_level_3":
                                {
                                    Address_administrative_area_level_3 = longname;
                                    break;
                                }


                            case "route":
                                {
                                    Address_streetname = longname;
                                    break;
                                }
                            default:
                                fHit = false;
                                break;
                        }

                    }

                    catch (Exception ex)
                    {
                        return null;
                    }


                }

                return (element.InnerText);
            }

        }
        catch (Exception ex)
        {
            return null;
        }
    }

    [Serializable]
    public class GeocoderLocation
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
        public override string ToString()
        {
            return String.Format("{0}, {1}", Latitude, Longitude);
        }
    }