简体   繁体   English

如何从SnappedPoints响应中提取Google Maps RoadsApi的纬度/经度?

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

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 我正在对RoadsApi使用Google Maps Java API:文档: 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: 这确实给了我预期的SnappedPoints []响应,但是我对如何解析它或获取字符串值有些困惑:

    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: 正如您在此API的文档页面上看到的那样,它几乎没有介绍Java实现,只是简单地指出响应返回如下:

{
  "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. 这大致模拟了我收到的响应,我只能想象他们正在使用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! 我需要的是经久耐用!

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. 首先,您应该有一个可以包装这些数据的类,因此该类应具有您可以从Google API响应中获取的应用程序中可能需要的所有字段。 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. 然后,您将需要一些可以从响应中获取数据的JSON解析器,最后,您必须使用JSON对象中的参数实例化先前创建的类。

For more detailed technical information, you may want to take a look at this answer . 有关更多详细的技术信息,您可能需要查看此答案

The Java client library for Google Maps Services is just a wrapper on top of the web service. Google Maps Services的Java 客户端库只是Web服务之上的包装。

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: 您检查了响应结构中的纯Web服务(它是JSON),但是客户端库将响应转换为Javadoc中描述的Java对象:

https://googlemaps.github.io/google-maps-services-java/v0.10.0/javadoc/index.html?overview-summary.html 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. 如果您在Javadoc中检查SnappedPoint类,您将看到它的location属性是LatLng对象。 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! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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