如何使用谷歌地图源和目标?

时间:2015-01-30 16:57:30

标签: android google-maps-android-api-2

我正在尝试创建如何使用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();
    }



}

1 个答案:

答案 0 :(得分:1)

如果您打算在来源&之间画一条线。 目的地,您可以使用 This Labrary

如果您想从来源目的地启动相机动画,我不确定您的代码是否正确!我认为你应该将目的地传递给 cameraUpdate ,而不是来源 ......

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(targetLat, targetLong), 10);