我正在尝试创建一个如何使用GoogleMap的方法。我要追踪源和目标的路线。为此,我有经度和纬度的源位置和目标位置。为此,我创建了两个属性。
source and target
源是我的位置,目标是我想去的地方。
我该怎么做?如何在GoogleMap中追踪路线?
public class FormComoChegarEmpresa extends Fragment{
private MapView mapView;
private GoogleMap map;
private final String TAG = getClass().getSimpleName() + "->";
private static Empresa empresa;
//latitude longitude
private Double sourceLat, sourceLong;
private Double targetLat, targetLong;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.como_chegar_empresa, container, false);
if(getArguments().getSerializable("empresa") != null){
empresa = (Empresa)getArguments().getSerializable("empresa");
targetLat = empresa.getEndereco().getLatitude();
targetLong = empresa.getEndereco().getLongitude();
}
mapView = (MapView)view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
if(map != null){
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
//my location
sourceLat = map.getMyLocation().getLatitude();
sourceLong = map.getMyLocation().getLongitude();
try {
MapsInitializer.initialize(this.getActivity());
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(sourceLat,
sourceLong), 10);
map.animateCamera(cameraUpdate);
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}