user2775241
user2775241

Reputation: 119

c#:converting address to longitude and latitude

i was trying to convert address into longitude and latitude using this code using geocoding api

protected void Button1_Click(object sender, EventArgs e)
{
    IGeocoder geocoder = new GoogleGeocoder() {  };
    Address[] addresses = geocoder.Geocode("#65/1 bangalore").ToArray();
    foreach (Address adrs in addresses)
    {
        Response.Write("address:"+adrs);
    }
}

the answer i got was the more detailed address not the longitude and latitude .for example for this code i got

address:Hari Om Trust Tapovana, 1, Bangalore, Karnataka 562123, India 

someone please tell me what modification i have to do so that i could get longitude and latitude?

the code for googlegeocoder is

 #region Assembly Geocoding.dll, v3.1.0.0
    // C:\Users\Admin\Documents\Visual Studio 2012\WebSites\learn1\Bin\Geocoding.dll
    #endregion

    using Geocoding;
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Threading;
    using System.Threading.Tasks;

    namespace Geocoding.Google
    {
        public class GoogleGeocoder : IGeocoder, IAsyncGeocoder
        {
            public GoogleGeocoder();

            public string ApiKey { get; set; }
            public Bounds BoundsBias { get; set; }
            public BusinessKey BusinessKey { get; set; }
            public string Language { get; set; }
            public WebProxy Proxy { get; set; }
            public string RegionBias { get; set; }
            public string ServiceUrl { get; }

            public IEnumerable<GoogleAddress> Geocode(string address);
            public Task<IEnumerable<GoogleAddress>> GeocodeAsync(string address);
            public Task<IEnumerable<GoogleAddress>> GeocodeAsync(string address, CancellationToken cancellationToken);
            public IEnumerable<GoogleAddress> ReverseGeocode(Location location);
            public IEnumerable<GoogleAddress> ReverseGeocode(double latitude, double longitude);
            public Task<IEnumerable<GoogleAddress>> ReverseGeocodeAsync(double latitude, double longitude);
            public Task<IEnumerable<GoogleAddress>> ReverseGeocodeAsync(double latitude, double longitude, CancellationToken cancellationToken);

            protected class RequestState
            {
                public readonly CancellationToken? cancellationToken;
                public readonly HttpWebRequest request;

                public RequestState(HttpWebRequest request, CancellationToken? cancellationToken);
            }
        }
    }

Upvotes: 0

Views: 10587

Answers (3)

Abhinav Ranjan Sinha
Abhinav Ranjan Sinha

Reputation: 31

Here is good and simple example:

Adress to Lat- long generation

Thanks

Upvotes: 0

Luc Morin
Luc Morin

Reputation: 5380

Ahh... quite simple in Fact:

protected void Button1_Click(object sender, EventArgs e)
{
    IGeocoder geocoder = new GoogleGeocoder() {  };
    Address[] addresses = geocoder.Geocode("#65/1 bangalore").ToArray();
    foreach (Address adrs in addresses)
    {
        Response.Write("address:"+adrs.Coordinates);
    }
}

Cheers

EDIT: The reason you're only seeing a street address is because of Address.ToString() being called by default in your loop.

Upvotes: 3

Sohail Ali
Sohail Ali

Reputation: 468

Refer to : Google has a geocoding API which seems to work pretty well for most of the locations that they have Google Maps data for.

http://googlemapsapi.blogspot.com/2006/06/geocoding-at-last.html

They provide online geocoding (via JavaScript):

http://code.google.com/apis/maps/documentation/services.html#Geocoding

Or backend geocoding (via an HTTP request):

http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct

The data is usually the same used by Google Maps itself. (note that there are some exceptions to this, such as the UK or Israel, where the data is from a different source and of slightly reduced quality)

Upvotes: 0

Related Questions