Domanda

Ho un requisito ai dati Geocode utilizzando il servizio di geocodifica di Google. servizi di geocodifica di Google non sono così amichevole per il consumo tramite NET come, ad esempio di Bing (nessuna sorpresa là) così mentre ho potuto andare tutti fuori con ContractDataSerializers, WCF, JSON e tutto un altro mucchio di acronimi c'è di sbagliato nulla con qualcosa come il qui sotto se tutti bisogno che è, diciamo, latitudine e longitudine vale a dire.

string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address=blah&region=ie&sensor=false", HttpUtility.UrlEncode(address));

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(url);
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/GeocodeResponse/result");

if (xmlNodeList != null)
{
   // Do something here with the information
}

Oltre a un grande sforzo di sviluppo in anticipo che cosa esattamente farà l'altro approccio buy? Sono molto confortevoli, con WCF, DataContracts, ServiceContracts ecc, ma non riesco a vedere quello che ti portano al tavolo qui ...

È stato utile?

Soluzione

Usa il progetto GoogleMap controllo su CodePlex: http://googlemap.codeplex.com/

E 'di classe per fare geocoding con Google: http: //googlemap.codeplex .com / WikiPage? title = Google% 20Geocoder & referringTitle = Documentazione .

Altri suggerimenti

userei XDocument con WebRequest. Seguendo esempio href="http://www.superstarcoders.com/blogs/posts/geocoding-in-c-sharp-using-google-maps.aspx" aiuto potrebbe.

public static GeocoderLocation Locate(string query)
{
    WebRequest request = WebRequest.Create("http://maps.google.com/maps?output=kml&q="
        + HttpUtility.UrlEncode(query));

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

            XNamespace ns = "http://earth.google.com/kml/2.0";

            XElement longitudeElement = document.Descendants(ns + "longitude").FirstOrDefault();
            XElement latitudeElement = document.Descendants(ns + "latitude").FirstOrDefault();

            if (longitudeElement != null && latitudeElement != null)
            {
                return new GeocoderLocation
                {
                    Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                    Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                };
            }
        }
    }

    return null;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top