问题描述
我已经使用Google Maps及其API创建了一个简单的应用程序。 我已经完成了从起点到终点的路线绘制工作,包括标记。
标记绘图
mOrigin = mMap.addMarker(new MarkerOptions()
.position(new LatLng(selectedOrigin.getLat(),selectedOrigin.getLon()))
.icon(BitmapDescriptorFactory.fromresource(R.drawable.ic_location_green)));
mDestination = mMap.addMarker(new MarkerOptions()
.position(new LatLng(selectedDestination.getLat(),selectedDestination.getLon()))
.icon(BitmapDescriptorFactory.fromresource(R.drawable.ic_location_green)));
路线图结果
ArrayList<LatLng> points;
polylineoptions lineOptions = new polylineoptions();
if(result == null || result.size() < 1){
Toast.makeText(parentActivity,"No Points",Toast.LENGTH_SHORT).show();
return;
}
points = new ArrayList<>();
for(int i = 0; i < result.size(); i++){
List<HashMap<String,String>> path = result.get(i);
for(int j = 0; j < path.size(); j++){
HashMap<String,String> point = path.get(j);
if(j == 0){ // Get distance from the list
mdistance = point.get("distance");
continue;
} else if(j == 1){ // Get duration from the list
mDuration = point.get("duration");
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat,lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(8);
lineOptions.geodesic(false);
lineOptions.color(ContextCompat.getColor(parentActivity,R.color.colorPrimary));
}
// Drawing polyline in the Google Map for the i-th route
if (polyLines !=null) polyLines.remove();
polyLines = mMap.addpolyline(lineOptions);
现在我的问题是,我想在移动到当前位置(我的新起点)时更新我的折线或点,而无需调用Google Directions-API再次。
更新当前位置
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult location) {
LatLng currentLocation = new LatLng(location.getLastLocation().getLatitude(),location.getLastLocation().getLongitude());
if (mOrigin == null) {
mOrigin = mMap.addMarker(new MarkerOptions()
.position(currentLocation)
.icon(BitmapDescriptorFactory.fromresource(R.drawable.ic_current_location)));
} else {
mOrigin.setPosition(currentLocation);
mOrigin.setIcon(BitmapDescriptorFactory.fromresource(R.drawable.ic_current_location));
}
//Update polyline/Points here
}
};
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)