【问题标题】:Get google map direction from current location to destination获取从当前位置到目的地的谷歌地图方向
【发布时间】:2015-11-04 16:27:48
【问题描述】:

我正在使用谷歌地图方向 API 来获取方向 json,但我的问题是我无法将当前位置传递给 origin 参数。有没有办法做到这一点?

http://maps.google.com/maps/api/directions/json?origin=41.393529,-72.811514&destination=41.311725,-72.740211

【问题讨论】:

  • url 格式看起来不错,它返回结果。究竟是什么问题?
  • 我想要原点的当前位置
  • 要获取当前位置,请查看以下答案:stackoverflow.com/a/30255219/4409409

标签: android google-maps google-maps-api-3 google-directions-api


【解决方案1】:

看看documentation。看起来您使用了错误的网址。例如,文档将 https://maps.googleapis.com/maps/api/directions/json?origin=Brooklyn&destination=Queens 列为正确的 API。那么也许将 maps.google.com 更改为 maps.googleapis.com?

编辑:

啊,现在我明白了。 Google Maps API 似乎没有提供此功能。猜猜最好的方法是使用 Android 手机上的位置管理器获取纬度和经度并在 url 中格式化。

【讨论】:

    【解决方案2】:

    如果您使用的是 google maps api,那么您必须在您的应用程序中使用 googleapiclient。如果没有,请这样做。

    使用 google api 客户端-

    googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .enableAutoManage(this, 0, this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .build();
    
    @Override
    protected void onStart() {
        googleApiClient.connect();
        super.onStart();
    }
    
    @Override
    protected void onStop() {
        if (googleApiClient != null && googleApiClient.isConnected()) {
            googleApiClient.disconnect();
        }
        super.onStop();
    }
    

    然后在连接到 google ai 客户端后,您将必须实现方法并在 onConnected() 方法内部(您必须覆盖此方法)您可以获得当前位置。

    在 onConnected 中获取当前位置-

    Location loc = LocationServices
                .FusedLocationApi
                .getLastLocation(googleApiClient);
    

    loc 对象是当前位置,您可以在任何代码点中使用该对象来获取该实例的位置,但只能在连接 googleapiclient 之后使用。

    你可以将它应用到你的代码中——

    String url="http://maps.google.com/maps/api/directions/json?origin="+loc.getLatitude()+","+loc.getLongitude()+"&destination=41.311725,-72.740211";
    

    【讨论】:

    • 如果这是您想要的,请您接受这个作为正确答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2012-12-22
    • 1970-01-01
    • 2012-11-26
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多