简体   繁体   中英

Google Map api doesn't gives lat/long on exact address c#

I'm trying to get latitude and longitude from address text using google map api.

Here is code, that I have tried.

First:

var locationService = new GoogleLocationService();
var point = locationService.GetLatLongFromAddress(business.Address2);

var latitude = point.Latitude;
var longitude = point.Longitude;

Second:

IGeocoder geocoder = new GoogleGeocoder() { };
Address[] addresses = geocoder.Geocode(business.Address2).ToArray();
foreach (Address adrs in addresses)
{
    var test = adrs.Coordinates;
} 

Third:

public RootObject GetLatLongByAddress(string address)
{
    var root = new RootObject();

    var url =string.Format(
                    "http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true_or_false", address);
    var req = (HttpWebRequest)WebRequest.Create(url);

    var res = (HttpWebResponse)req.GetResponse();

    using (var streamreader = new StreamReader(res.GetResponseStream()))
    {
        var result = streamreader.ReadToEnd();

        if (!string.IsNullOrWhiteSpace(result))
        {
           root = JsonConvert.DeserializeObject<RootObject>(result);
         }
    }
        return root;
 }    

And my address string is "Ground Floor, Nishika Avenue, Swastik Char Rasta, Navrangpura, Opposite Narnarayan Complex, Ahmadabad"

I've tried with all though, but it not gives me latitude and longitude anyway.

When I remove first part of my address string (ie Navrangpura, Opposite Narnarayan Complex, Ahmadabad ) and try, it's give me lat/long.

But when I search with full address on Google Map it shows the result.

Why this is so? Am i missing something?

The address 'Ground Floor, Nishika Avenue, Swastik Char Rasta, Navrangpura, Opposite Narnarayan Complex, Ahmadabad' cannot be found at maps.google.com as well

在此输入图像描述

The API cannot find something that doesn't exist. You should add this place via maps.google.com or format your address string better following the

https://developers.google.com/maps/faq#geocoder_queryformat

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM