如何使用 GoogleMap 源和目标?
how to get using GoogleMap source and target?
我正在尝试创建如何使用 GoogleMap。我想要源和目标的跟踪路线。为此,我有经度和纬度的源位置和目标位置。为此,我创建了 2 个属性。 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();
}
}
如果要在 source 和 destination 之间画一条线,可以使用 This Labrary
如果您想要从 source 到 destination 开始相机动画,我不确定您的代码是否正确!只是我认为你应该将 destination 传递给 cameraUpdate,而不是 source ...
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(targetLat, targetLong), 10);
我正在尝试创建如何使用 GoogleMap。我想要源和目标的跟踪路线。为此,我有经度和纬度的源位置和目标位置。为此,我创建了 2 个属性。 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();
}
}
如果要在 source 和 destination 之间画一条线,可以使用 This Labrary
如果您想要从 source 到 destination 开始相机动画,我不确定您的代码是否正确!只是我认为你应该将 destination 传递给 cameraUpdate,而不是 source ...
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(targetLat, targetLong), 10);