如何像谷歌地图一样在地图上显示交通和估计时间?



我正在做一个项目,我想在地图上显示道路交通和时间估计,比如Googlemap。 .![喜欢这张图片 ]1

if (direction.isOK()) {
Route route = direction.getRouteList().get(0);
ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
polyline = mGoogleMap.addPolyline(DirectionConverter.createPolyline(mActivity, directionPositionList, 5
, Color.BLUE));
setCameraWithCoordinationBounds(route);
Leg leg = route.getLegList().get(0);
Info distanceInfo = leg.getDistance();
Info durationInfo = leg.getDuration();
String distance = distanceInfo.getText();
String duration = durationInfo.getText();
}

如果您使用的是谷歌地图SDK,则可以在应用获得您的位置后启用路况详细信息。

@Override
public void onMyLocationChange(Location arg0) {
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Here"));
//load the traffic now
googleMap.setTrafficEnabled(true);
}
});

您也可以尝试以下代码。它会正确初始化地图,然后在检测到您的当前位置后设置交通数据。

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
//load the traffic now
googleMap.setTrafficEnabled(true);
}
});
}
}
}

最新更新