Googlelocationservice problem getting latitude and longitude

Asked

Viewed 465 times

-1

Follows the code:

Try 1:(It works)

var address = "Osasco - SP, Brasil";
var locationService = new GoogleLocationService();
var point = locationService.GetLatLongFromAddress(address);

Attempt 2: (Does not work)

var address = "uol - Rua dos Autonomistas - Santa Paula, São Caetano do Sul - SP, Brasil";
var locationService = new GoogleLocationService();
var point = locationService.GetLatLongFromAddress(address);

Because on the second try, the point variable is null ? Null point variable cannot get lat and long.

I’ve tried to get lat and long through that website. Still doesn’t work with that name :

Uol - Rua dos Autonomistas - Santa Paula, São Caetano do Sul - SP, Brazil

By google maps Official he finds with lat and long: https://www.google.com/maps/place/uol/@-23.6146064,-46.5673915,21z/data=! 4m5! 3m4! 1s0x94ceff43154a6537:0x90bb93302710c14d! 8m2! 3d-23.6145972! 4d-46.5672306? hl=en

Some brilliant solution ?

  • It depends on the format of the rest of the data. If it is always a piece that is not part of the address at the beginning, just discard.

  • You will have to look at the Googlelocationservice documentation. What are the conditions for the method locationService.GetLatLongFromAddress(string) returns null.

1 answer

3


The API finds nothing because the address you are passing is "invalid".

Your address is invalid to the requested parameters because you are passing a part that is not part of the address, which is Uol -. This part is the location you want (I believe), but it is not an address for the google API.

Through Google Maps it locates because the system has a greater intelligence, so to speak, than the API itself. If you search the full address on google, will see that you find a registered company, and with that the address is valid. For the API, searching by address, is not.

See this example searching with Uol -.

In this example the result is:

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}

Now, if you just remove the Uol -, will see the following result:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Rua dos Autonomistas",
               "short_name" : "R. dos Autonomistas",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Santa Paula",
               "short_name" : "Santa Paula",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "São Caetano do Sul",
               "short_name" : "São Caetano do Sul",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "São Paulo",
               "short_name" : "SP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Brasil",
               "short_name" : "BR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "09520-040",
               "short_name" : "09520-040",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "R. dos Autonomistas - Santa Paula, São Caetano do Sul - SP, 09520-040, Brasil",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -23.6134371,
                  "lng" : -46.5672122
               },
               "southwest" : {
                  "lat" : -23.6152546,
                  "lng" : -46.5674237
               }
            },
            "location" : {
               "lat" : -23.6145817,
               "lng" : -46.5673196
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : -23.6129968697085,
                  "lng" : -46.5659689697085
               },
               "southwest" : {
                  "lat" : -23.6156948302915,
                  "lng" : -46.5686669302915
               }
            }
         },
         "place_id" : "ChIJD0gD3e5czpQRQnEjTXrZaPE",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}

Note that in the end google even returned a place_id, that is:

"place_id" : "ChIJD0gD3e5czpQRQnEjTXrZaPE",

If you use the Reverse Geocoding by Place ID, you will see the exact location of it, and it seems to me that’s the place you’re looking for.

Correct me if I’m wrong, but I think you’re using the package Googlemaps.Locationservices.

It works for some things, but I think for your case will not suit you perfectly. You can see if the Google’s own API package Answer him, or you can find a new one in Nuget and who knows, make your own or use a simple Webclient to return the browser JSON, this way:

using (WebClient client = new WebClient())
{
    var address = "Rua dos Autonomistas - Santa Paula, São Caetano do Sul - SP, Brasil";
    var url = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}", address);
    var result = client.DownloadString(url);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.