在Android中使用谷歌地图创建一个活动,以显示两个位置之间的路线

时间:2016-10-26 05:43:29

标签: android google-maps

我的变量中有两个位置点,我想显示这两个位置之间路线的地图。 我只想要一张谷歌地图,其中包含位置之间的路线。

2 个答案:

答案 0 :(得分:1)

你可以用它 Android-GoogleDirectionLibrary

要添加此库,请在gradle中添加依赖项

compile 'com.akexorcist:googledirectionlibrary:1.0.4'

示例代码

    GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
            .from(new LatLng(37.7681994, -122.444538))
                .to(new LatLng(37.7749003,-122.4034934))
                .avoid(AvoidType.FERRIES)
                .avoid(AvoidType.HIGHWAYS)
                .execute(new DirectionCallback() {
        @Override
        public void onDirectionSuccess(Direction direction, String rawBody) {
            if(direction.isOK()) {
                // Do something
Snackbar.make(btnRequestDirection, "Success with status : " + direction.getStatus(), Snackbar.LENGTH_SHORT).show();
              googleMap.addMarker(new MarkerOptions().position(origin));
            googleMap.addMarker(new MarkerOptions().position(destination));

            ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
            googleMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.RED));

        }

            } else {
                // Do something
            }
        }

        @Override
        public void onDirectionFailure(Throwable t) {
            // Do something
        }
    });

答案 1 :(得分:0)

  1. 在您的控制台中启用Google Maps Directions API
  2. 您可以将该代码传输到地图侦听器EG。

    @Override
        public void onMapClick(LatLng latLng) {
            GoogleDirectionConfiguration.getInstance().setLogEnabled(true);
              $key = getResources().getString(R.string.google_maps_key; 
    
            GoogleDirection.withServerKey($key))
                .from(mCurrLocationMarker.getPosition())
                .to(latLng)
                .transportMode(TransportMode.DRIVING)
                .transitMode(TransitMode.BUS)
                .unit(Unit.METRIC)
                .execute(new DirectionCallback() {
                    @Override
                    public void onDirectionSuccess(Direction direction, String rawBody) {
    
                        Log.d("GoogleDirection", "Response Direction Status: " + direction.toString()+"\n"+rawBody);
    
                        if(direction.isOK()) {
                            // Do something
                            Route route = direction.getRouteList().get(0);
                            Leg leg = route.getLegList().get(0);
                            ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
                            PolylineOptions polylineOptions = DirectionConverter.createPolyline(getApplication(), directionPositionList, 5, Color.RED);
                            mMap.addPolyline(polylineOptions);
                        } else {
                            // Do something
                        }
                    }
    
                    @Override
                    public void onDirectionFailure(Throwable t) {
                        // Do something
                        Log.e("GoogleDirection", "Response Direction Status: " + t.getMessage()+"\n"+t.getCause());
                    }
                });
    }