代码之家  ›  专栏  ›  技术社区  ›  林果皞

Android Google map-如果找不到目的地方向路线,则返回

  •  0
  • 林果皞  · 技术社区  · 7 年前

    String uri = "http://maps.google.com/maps?daddr=" + destLat + "," + destLong + " (" + safeURLString + ")"; //first way
    //String uri = "https://www.google.com/maps/search/?api=1&query=" + destLat + "," + destLong; //second way
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    intent.setPackage("com.google.android.apps.maps");
    if (intent.resolveActivity(getPackageManager()) != null) {
        startChildActivity(intent);
    }
    

    问题是有时第一条路能找到路线,有时只有第二条路能在没有路线时标记目的地。

    是否可以检查是否未找到路由,然后回退/回调以尝试第二个url或至少标记目标?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrii Omelchenko    7 年前

    HttpUrlConnection Directions API 要测试路由是否存在:

    https://maps.googleapis.com/maps/api/directions/json?origin=<originLat>,<originLon>&destination=<destLat>,<destLon>&key=<YOUR_DIRECTIONS_API_KEY>
    

    如果

    {
       "geocoded_waypoints" : [ {}, {} ],
       "routes" : [],
       "status" : "ZERO_RESULTS"
    }
    

    {
       "geocoded_waypoints" : [
    
       <lines of response here> 
       ...
    
       ],
       "status" : "OK"
    }
    ...
    

    路由存在,您可以使用第一种方法查找路由。所以使用 方向API 通过 检查一下 "status" "status" : "OK"