本文介绍了如何借鉴谷歌地图在android系统的动态行驶路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void initalizeMap()
{
     // Google Play Services are available
           // Getting GoogleMap object from the fragment
            googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

            if(gps.canGetLocation())
            {
                  double latitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LATITUDE));
                  double longtitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LONGITUDE));

                  Log.d("getting gps value", ""+ latitude);
                  Log.d("getting gps Long", ""+ longtitude);
                // Creating a LatLng object for the current location
                LatLng latLng = new LatLng(latitude, longtitude);
                // create marker
                marker = new MarkerOptions().position(latLng).title("You are here!");
                // Changing marker icon
                // set yours icon here
                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
                // Showing the current location in Google Map
                googleMap.setTrafficEnabled(true);
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                // Zoom in the Google Map
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));
                googleMap.addMarker(marker);
            }else{
                Toast.makeText(getActivity(), "failed To get the location", Toast.LENGTH_SHORT).show();
            }
    }

    @Override
public void onLocationChanged(Location location) {

        double latitude = gps.getLatitude();
        // Getting longitude of the current location
        double longitude = gps.getLongitude();
        // Creating a LatLng object for the current location

        Log.d("latitude map Class", "latitude:  " + gps.getLatitude());
        Log.d("longitude Map CLass", "longitude:  " + gps.getLongitude());

        LatLng latLng = new LatLng(latitude, longitude);
        // create marker
        MarkerOptions marker = new MarkerOptions().position(latLng).title("You are here!");
        // Changing marker icon
        // set yours icon here
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));

}
解决方案

In order to get the drive or walking time on Google maps you have to make use of the Direction API's provided by Google. The API call for the Direction would would something like this:

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los%20Angeles,CA&sensor=false

And you can embed that inside a class where you would make an http call using AsyncTask which would look like this:

GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
url.put("origin", origin);
url.put("destination", destination);
url.put("sensor",false);

Please follow this tutorial or this in order get the direction API (that includes driving, transit. walking and bicycling direction on your maps. )

Hope that would help!!!

这篇关于如何借鉴谷歌地图在android系统的动态行驶路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 13:32