使用 Retrofit 的 Google 地图方向 API

2024-04-28

我想绘制两个位置之间的路线。我使用retrofit库来调用API。我没有得到任何回应。我需要 ArrayList 中的折线。我怎么做到这一点?还需要帮助来创建 GsonAdapter... 谢谢..


在活动中`

     String base_url = "http://maps.googleapis.com/";

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(base_url)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    MyApiRequestInterface reqinterface = restAdapter.create(MyApiRequestInterface.class);

    reqinterface.getJson(fromPosition.latitude + "," + fromPosition.longitude, toPosition.latitude + "," + toPosition.longitude, new Callback<DirectionResults>() {


        @Override
        public void success(DirectionResults directionResults, Response response) {
            Log.i("zacharia", "inside on success" +directionResults.getRoutes().size());
            ArrayList<LatLng> routelist = new ArrayList<LatLng>();
            if(directionResults.getRoutes().size()>0){
                ArrayList<LatLng> decodelist;
                Route routeA = directionResults.getRoutes().get(0);
                Log.i("zacharia", "Legs length : "+routeA.getLegs().size());
                if(routeA.getLegs().size()>0){
                    List<Steps> steps= routeA.getLegs().get(0).getSteps();
                    Log.i("zacharia","Steps size :"+steps.size());
                    Steps step;
                    Location location;
                    String polyline;
                    for(int i=0 ; i<steps.size();i++){
                        step = steps.get(i);
                        location =step.getStart_location();
                        routelist.add(new LatLng(location.getLat(), location.getLng()));
                        Log.i("zacharia", "Start Location :" + location.getLat() + ", " + location.getLng());
                        polyline = step.getPolyline().getPoints();
                        decodelist = RouteDecode.decodePoly(polyline);
                        routelist.addAll(decodelist);
                        location =step.getEnd_location();
                        routelist.add(new LatLng(location.getLat() ,location.getLng()));
                        Log.i("zacharia","End Location :"+location.getLat() +", "+location.getLng());
                    }
                }
            }
            Log.i("zacharia","routelist size : "+routelist.size());
            if(routelist.size()>0){
                PolylineOptions rectLine = new PolylineOptions().width(10).color(
                        Color.RED);

                for (int i = 0; i < routelist.size(); i++) {
                    rectLine.add(routelist.get(i));
                }
                // Adding route on the map
                mMap.addPolyline(rectLine);
                markerOptions.position(toPosition);
                markerOptions.draggable(true);
                mMap.addMarker(markerOptions);
            }
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            System.out.println("Failure, retrofitError" + retrofitError);
        }
    });`

创建改造接口

public interface MyApiRequestInterface {

@GET("/maps/api/directions/json")
public void getJson(@Query("origin") String origin,@Query("destination") String destination, Callback<DirectionResults> callback);}

创建模型类

public class DirectionResults {
@SerializedName("routes")
private List<Route> routes;

public List<Route> getRoutes() {
    return routes;
}}
 public class Route {
@SerializedName("overview_polyline")
private OverviewPolyLine overviewPolyLine;

private List<Legs> legs;

public OverviewPolyLine getOverviewPolyLine() {
    return overviewPolyLine;
}

public List<Legs> getLegs() {
    return legs;
}
}

public class Legs {
private List<Steps> steps;

public List<Steps> getSteps() {
    return steps;
}
}

public class Steps {
private Location start_location;
private Location end_location;
private OverviewPolyLine polyline;

public Location getStart_location() {
    return start_location;
}

public Location getEnd_location() {
    return end_location;
}

public OverviewPolyLine getPolyline() {
    return polyline;
}
}

public class OverviewPolyLine {

@SerializedName("points")
public String points;

public String getPoints() {
    return points;
}
}

public class Location {
private double lat;
private double lng;

public double getLat() {
    return lat;
}

public double getLng() {
    return lng;
}
}

创建折线解码方法

public class RouteDecode {

public static ArrayList<LatLng> decodePoly(String encoded) {
    ArrayList<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;
    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
        poly.add(position);
    }
    return poly;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Retrofit 的 Google 地图方向 API 的相关文章

随机推荐

  • 将列的百分比设置为 0 (pandas)

    我有一个 pandas 数据框 我想将列的某些百分比设置为 0 假设 df 有两列 A B 1 6 2 7 3 8 4 4 5 9 我现在想将 df 的前 20 和后 20 的 B 设置为 0 A B 1 0 2 7 3 8 4 4 5 0
  • 通过 DFS 查找图中的强连通分量

    我正在阅读有关 BFS 和 DFS 的图算法 当我分析通过DFS在图中查找强连通分量的算法时 我想到了一个疑问 为了找到强连通分量 书 Coremen 做了什么 首先它在图上运行 DFS 以获得顶点的完成时间 然后再次以完成时间的降序在图的
  • 如何“安装”自定义 Windows 驱动程序?

    我计划用 C 语言编写一个基本的 Windows 注册表过滤器 该过滤器的目的是挂钩所有 用户和内核特权 注册表调用 以便我可以在我的程序中使用它们 我基本上是复制 Mark Rusinovich 的 regmon 进程监视器 但更基本 我
  • pdfBox 返回错误的编码字符

    我有一个pdfhttp www persianacademy ir UserFiles File fe1394 pdf http www persianacademy ir UserFiles File fe1394 pdf我想从中提取单词
  • Bash shell 中的“[ ]”与“[[ ]]”[重复]

    这个问题在这里已经有答案了 这可能已经得到回答 但我还是要问 我有两个版本的脚本 comp sh bin sh export tDay date Y m d newfile filename tDay filename filename 2
  • 直接加载页面到锚标记

    当我加载 URL 中带有哈希标记的页面时 页面会加载 然后跳转到锚标记 有没有什么方法可以防止这种 跳转 要么直接将页面加载到锚标记 要么至少使滚动平滑 我在 Chrome 和 Firefox 中看到这个问题 但在 IE 中没有 如果你仍然
  • Jersey 2 - ContainerRequestFilter get 方法注解

    我试图获取 ContainerRequestFilter 对象中的方法注释 控制器 GET RolesAllowed ADMIN public String message return Hello rest12 容器请求过滤器 Provi
  • 如何更改Android中开关的文本颜色

    我正在创建一个使用 Android 4 0 的应用程序 我想知道是否可以更改开关中文本的文本颜色 我尝试过设置文本颜色 但不起作用 有任何想法吗 提前致谢 你必须使用android switchTextAppearance属性 例如 and
  • 为什么即使优化级别为 3,向量分配也需要花费这么多时间?

    以前我在这里问过类似的问题 Android NDK vector resize 太慢 与分配有关 https stackoverflow com q 58745415 5709159 问题是这段代码 include
  • java中的内联初始化块

    我有课 public class MyMain public static void main String arg Temp t new Temp System out println instance initialize class
  • 加载 XSLT 文件时解析相对路径

    我需要使用 Apache FOP 进行 XSL 转换 我的代码如下 Setup FOP Fop fop fopFactory newFop MimeConstants MIME PDF out Setup Transformer Sourc
  • 在视图之间传递变量 SwiftUI

    再次基本问题 我想让变量 anytext 对于我要添加的所有未来视图都可见且可访问 在我的例子中 变量将是String 如果是的话 程序会改变吗 Float 我怎样才能将其另存为全局变量 如果我重新启动应用程序 变量会自行删除吗 如何保存即
  • 一起使用similar_text和strpos

    我想创建一个简单的搜索引擎 在用户输入中查找关键字 我知道我可以使用 strpos 来检查字符串中是否存在单词 但是 我希望用户能够拼写错误的单词 例如 userInput What year did George Washingtin b
  • 使用 IcyStreamMeta 从 SHOUTcast 获取元数据

    我正在为 Android 编写一个应用程序 从 SHOUTcast mp3 流中获取元数据 我正在使用我在网上找到的一个非常漂亮的类 我稍微修改了一下 但我仍然有两个问题 1 我必须使用 TimerTask 不断 ping 服务器来更新元数
  • Keras TimeDistributed Conv1D 错误

    这是我的代码 cnn input Input shape cnn max length emb output Embedding num chars 1 output dim 32 input length cnn max length t
  • 如何为启动的 setup.exe 创建日志文件

    我继承了一些InstallShield InstallScript项目 我目前正在使用InstallShield 2009 当我运行 setup exe 时 我似乎无法创建日志文件 我需要指定哪些命令行选项 InstallShield 有一
  • Makefile 头依赖项

    我是使用 make 的新手 并且一直在通过以下方式学习基础知识本教程 http www cs colby edu maxwell courses tutorials maketutor 这是本教程中的最后一个 makefile 示例 IDI
  • 使用 6.0 API (Android) 从服务器发送和接收数据

    我真的很困惑 我正在尝试开发一个简单的功能 允许我从服务器发送和接收数据 操作如下 在一个活动中 我对服务器上的 PHP 文件执行 HTTP POST PHP 文件 获取我发送的数据 通常是字符串 并使用通过 http 发送的参数执行查询
  • Eclipse 构建 Android 应用程序:如何在编译时创建两个版本?

    我正在编写一个 Android 应用程序 并希望基于相同的代码创建两个版本 免费版本和高级版本 我有两个版本的一个代码库 具有各种运行时检查来启用或禁用某些功能 例如 public class MyAppContext extends Ap
  • 使用 Retrofit 的 Google 地图方向 API

    我想绘制两个位置之间的路线 我使用retrofit库来调用API 我没有得到任何回应 我需要 ArrayList 中的折线 我怎么做到这一点 还需要帮助来创建 GsonAdapter 谢谢 在活动中 String base url http