sexta-feira, 24 de julho de 2015

GeoLocation - Simple

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Net;
using System.IO;

namespace Lapuinka.Util
{
    public class Geolocalizacao
    {
        ///
        /// Pesquisa longitude e latitude de um endereço utilizando api do googlemaps
        ///

        public string[] pesquisaGeolocalizacao(string rua, string bairro, string cidade, string uf, string cep)
        {
            //Rua José Loureiro, 282 - Centro, Curitiba - PR, 80010-000
            string endereco = rua + " - " + bairro + ", " + cidade + " - " + uf + ", " + cep + ", Brasil";

            var url = String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&region=br&sensor=false", endereco);
            string[] localizacoes;
            localizacoes = new string[2];

            string[] retono = pgComplemento(url);
            if (retono[0] == null)
            {
                endereco = bairro + ", " + cidade + " - " + uf + ", " + cep + ", Brasil";
                var url2 = String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&region=br&sensor=false", endereco);

                retono = pgComplemento(url2);
                if (retono[0] == null)
                {
                    endereco = cidade + " - " + uf + ", Brasil";
                    var url3 = String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&region=br&sensor=false", endereco);

                    retono = pgComplemento(url3);

                    localizacoes[0] = retono[0];
                    localizacoes[1] = retono[1];
                }else{
                    localizacoes[0] = retono[0];
                    localizacoes[1] = retono[1];
                }
            }else{
                localizacoes[0] = retono[0];
                localizacoes[1] = retono[1];
            }
            return localizacoes;
        }

        ///
        /// Complemento do método pesquisaGeolocalizacao
        ///

        private string[] pgComplemento(string url)
        {
            var req = (HttpWebRequest)WebRequest.Create(url);

            using (var resp = req.GetResponse())
            {
                using (var stream = new StreamReader(resp.GetResponseStream()))
                {
                    var data = System.Web.Helpers.Json.Decode(stream.ReadToEnd());
                    string[] localizacoes;
                    localizacoes = new string[2];

                    if (data.status == "OK")
                    {
                        localizacoes[0] = Convert.ToString(data.results[0].geometry.location.lng);
                        localizacoes[1] = Convert.ToString(data.results[0].geometry.location.lat);
                    }
                    return localizacoes;
                }
            }
        }

        ///
        /// Retorna distância em linha reta entre dois endereços. Esta pesquisa é feita utilizando longitude e latitude
        ///

        public static double PesquisaDistancia(double lat1, double long1, double lat2, double long2)
        {
            const double d2r = Math.PI / 180;

            double dlong = (long2 - long1) * d2r;
            double dlat = (lat2 - lat1) * d2r;
            double a = Math.Pow(Math.Sin(dlat / 2.0), 2) + Math.Cos(lat1 * d2r) * Math.Cos(lat2 * d2r) * Math.Pow(Math.Sin(dlong / 2.0), 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double distancia = 6367 * c;

            return distancia;
        }
    }

}



Another Geocode  



private String getGeocode(String address)
        {
            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            address = address.Replace(" ", "+");
            String url = "http://maps.google.com.br/maps/geo?q=" + address + "&output=csv&oe=utf8&sensor=false&key=YOURKEY";

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(url);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?

           
            return sb.ToString();
        }
    }
Postar um comentário