【问题标题】:how to draw dynamic travel path on google map in android如何在android中的谷歌地图上绘制动态旅行路径
【发布时间】:2015-01-03 03:27:24
【问题描述】:

我已经在 android 中集成了谷歌地图。我正在使用 GPS 获取当前位置。在 Onlocationchanged 上,我正在更新当前位置。

我怀疑我在 Google 地图 UI 上。我想从我当前的位置作为起点步行或开车,我想开始在地图上动态地画线。

我在网上搜索了很长时间,但我没有得到任何解决方案。这是我的代码,请任何人帮助我解决这个问题。

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));

}

我尝试使用以下链接 [1 st lnikn second link

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    为了在 Google 地图上获取驾车或步行时间,您必须使用 Google 提供的 Direction API。 Direction 的 API 调用会是这样的:

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

    您可以将其嵌入到一个类中,您可以在其中使用 AsyncTask 进行 http 调用,如下所示:

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

    请按照此tutorialthis 获取方向API(包括地图上的驾车、公交、步行和骑自行车方向。)

    希望对你有帮助!!!

    【讨论】:

    • 嘿,非常感谢。使用第一个链接我得到了解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多