How to extract the Lat/Long from Google Maps RoadsApi from a SnappedPoints response?

559 Views Asked by At

I am using the Google Maps Java API for the RoadsApi: Docs: https://developers.google.com/maps/documentation/roads/intro API: https://github.com/googlemaps/google-maps-services-java

My request is structured like this:

    latlngStart = new LatLng(Double.parseDouble(lastLoc.getNormalized_lat().toString()), Double.parseDouble(lastLoc.getNormalized_lng().toString()));
    LatLng latlngEnd   = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
    SnappedPoint[] points;

    if (origin.equals(true)) {
        points = RoadsApi.snapToRoads(context, latlngStart, latlngEnd).await();
    } else {
        points = RoadsApi.snapToRoads(context, latlngEnd).await();
    }

This does give me the expected SnappedPoints[] response, however I am somewhat at a loss of how to parse it or get the string values:

    for (SnappedPoint point : points) {
        System.out.println(point);
    }

This above piece of code iterates the response and prints out the contents:

[42.64472036,-83.21762969, placeId=ChIJf2D4OTbAJIgR3qx12TMRuIE, originalIndex=0]

As you can see on the doc page for this API, it says very little about the Java implementation, and simply states that the response comes back like this:

{
  "snappedPoints": [
    {
      "location": {
        "latitude": -35.2784167,
        "longitude": 149.1294692
      },
      "originalIndex": 0,
      "placeId": "ChIJoR7CemhNFmsRQB9QbW7qABM"
    },
...
}

This loosely mimics the response I received, and I can only imagine that theirs is using the JavaScript API.

Has anyone had any luck with this in the past? Or maybe have an idea about how to extrapolate the data I need from this response? I could parse it out of the string object, but that would be clunky, and unreliable at best - as I am not sure how it would behave if there were multiple records returned.

All I need is the lat and long!

2

There are 2 best solutions below

0
xomena On BEST ANSWER

The Java client library for Google Maps Services is just a wrapper on top of the web service.

You checked the response structure for pure web service that is a JSON, but the client library transforms response to Java objects that are described in javadoc:

https://googlemaps.github.io/google-maps-services-java/v0.10.0/javadoc/index.html?overview-summary.html

If you check the javadoc for SnappedPoint class you will see that it has a location property that is LatLng object. So you need to do the following in order to obtain coordinates

for (SnappedPoint point : points) {
    LatLng loc = point.location;
    double lat = loc.lat;
    double lng = loc.lng;
}  

I hope this helps!

0
zeroDivider On

First, you should have a class that can wrap that data, so the class should have all of the fields you may need in your app that you can get from Google API response. Then, you'll need some JSON parser that can get data from response and at the end you'll have to instantiate previously created class with arguments from JSON object.

For more detailed technical information, you may want to take a look at this answer.