1 (edited by mikejm 2021-12-07 08:40:39)

Topic: Get Lat/Long for Google Autocomplete or Route results?

1) Lat/Long from Google Autocomplete:

If I run a Google autocomplete query, it will return results in the format of OnlineMapsGooglePlacesAutocompleteResult, which gives me:

- string description - human readable address
- string reference - a reliable string identifier for the address
- string id - deprecated
- string place_id - an identifier that can be passed into Places API to get info about location

If I want to get the long/latitude for each location, I presume the most efficient way would be to use the place_id somehow. Is there any built in way or simple code I can use that will turn a place_ID into a lat/long so I can set a marker on map to it? Are there any built in methods?

I know we have geocoding by name here: https://infinity-code.com/atlas/online- … ample.html

One could use "description" into this geocoding method but in principle this will not be as correct as using place_id or reference from what I understand. Perhaps this doesn't matter and passing in the description is good enough.

2) Long/Lat from Directions Request:
Second if I have a route request from a Directions request which returns as follows:

 private void OnGoogleDirectionsComplete(string response) { //for drawing routes
        Debug.Log(response);

        // Try load result
        OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
        if (result == null || result.routes.Length == 0) return;

        // Get the first route
        OnlineMapsGoogleDirectionsResult.Route route = result.routes[0];

        // Draw route on the map
        OnlineMapsDrawingElementManager.RemoveAllItems();
        OnlineMapsDrawingElementManager.AddItem(new OnlineMapsDrawingLine(route.overview_polyline, Color.red, 2));

Is it possible to easily get from "route" the destination long/lat? Ie. if "route" contains a list of long/lat values to compose the route, how do I get the last entry to the route long/lat (which will be the destination)?

I can see in the Debug output it provides as part of "response":
   <end_location>
     <lat>______</lat>
     <lng>______</lng>
    </end_location>

How do I access this end location lat/long from the code above?

Thanks as always. I almost have all my functions working thanks to your help so I won't hopefully be bugging you too much longer. smile

Re: Get Lat/Long for Google Autocomplete or Route results?

Hello.

1. Google Autocomplete API does not return prediction locations.
To get full location information, you need to make a request to Google Places Details API.
https://infinity-code.com/doxygen/onlin … b9795d0c1d

2.

OnlineMapsVector2d endLocation = result.routes[0].legs.Last().end_location;

or

OnlineMapsVector2d endLocation = result.routes[0].overview_polylineD.Last();
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.