【问题标题】:Drawing path between two markers Google Maps V2两个标记之间的绘制路径 Google Maps V2
【发布时间】:2015-09-19 01:34:31
【问题描述】:

我有两个标记,一个是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
        }

    });

现在我只需要在两个标记之间画一条线。谢谢!

【问题讨论】:

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


    【解决方案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 修复您的原始纬度/经度,并在道路上获得更顺畅的路径。它目前没有 Android API,因此您可能需要使用 Web API 来访问 snap to road 服务。

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

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

    【讨论】:

    • 嘿,我先试试这个!非常感谢 :-) 真的很感激!
    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 2013-11-20
    • 2021-04-16
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多