绘制两个标记谷歌地图V2之间的路径

时间:2015-07-01 18:32:15

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

我有两个标记,即startLocation,另一个是stopLocation。 startLocation将检测用户的当前位置,然后用户将行走,当他们停止时,他们将按下停止并且stopLocation将被捕获为他们的新当前位置。我想在用户从startLocation移动到stopLocation时绘制折线。 或者,也可以在创建了开始和停止位置的两个标记之后绘制折线 - 无论哪个更可实现。 如何才能做到这一点?大多数答案都是指检索路线然后绘制折线,但这不是我想要的 - 我想获得用户的个性化路线。简而言之,我想记录用户所采用的路线。我已经设法创建了两个标记:

btnStart = (Button) findViewById(R.id.btnStart);
    btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // Create Start Marker
            // get current location
            LocationManager locManager;
            String context = Context.LOCATION_SERVICE;
            locManager = (LocationManager) getSystemService(context);

            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            c.setAltitudeRequired(false);
            c.setBearingRequired(false);
            c.setCostAllowed(true);
            c.setPowerRequirement(Criteria.POWER_LOW);

            String provider = locManager.getBestProvider(c, true);
            Location loc = locManager.getLastKnownLocation(provider);

            LatLng currentPosition = updateWithNewLocation(loc);

            Marker startLocation = map.addMarker(new MarkerOptions()
                    .position(currentPosition)
                    .title("Start Location")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));


        }

    });

    btnStop = (Button) findViewById(R.id.btnStop);
    btnStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // Create Stop

            // get current location
            LocationManager locManager;
            String context = Context.LOCATION_SERVICE;
            locManager = (LocationManager) getSystemService(context);

            Criteria c = new Criteria();
            c.setAccuracy(Criteria.ACCURACY_FINE);
            c.setAltitudeRequired(false);
            c.setBearingRequired(false);
            c.setCostAllowed(true);
            c.setPowerRequirement(Criteria.POWER_LOW);

            String provider = locManager.getBestProvider(c, true);
            Location loc = locManager.getLastKnownLocation(provider);

            LatLng currentPosition = updateWithNewLocation(loc);

            Marker stopLocation = map.addMarker(new MarkerOptions()
                    .position(currentPosition)
                    .title("Stop Location")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17));

            // Draw dynamic line
        }

    });

现在我需要的是在两个标记之间绘制线条。谢谢!

1 个答案:

答案 0 :(得分:1)

如果不跟踪用户的位置,则无法执行此操作。您应该使用requestLocationUpdates功能收听并获取用户位置的更新。有关收听GPS位置的详细信息,请参阅the developer guide

String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or, use GPS location data:
// String locationProvider = LocationManager.GPS_PROVIDER;

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

您可能还想使用newly released Google Maps Road API中的对齐道路功能来修复GPS中的原始lat / lng,并在路上获得更平滑的路径。它目前没有Android API,因此您可能需要使用Web API来访问对齐服务。

https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836
        &interpolate=true
        &key=API_KEY

用户停止跟踪或到达终点后,您可以create a polyline根据其路径进行操作。

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

如果不清楚,请告诉我,希望它有所帮助。