安卓谷歌地图标记不断点击同一点

3
当我在地图上点击某个位置时,应用程序会在当前位置和目的地(点击)之间绘制折线。我希望目的地标记可以自动点击(每隔几秒钟一次),以便在用户移动时绘制新的折线(更新最后一个)。我尝试使用处理程序和其他方法,但没有找到解决方案。任何答案都将不胜感激。
private ArrayList<LatLng> mClickedPoints;

/** Setting onclick listener for the map */
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {

        /**
         *  Clearing map if user clicks on map more then one time.
         * Reseting View widgets and arrays, adding points of interests
         */
        if (mClickedPoints.size() > 1) {
            mMap.clear();
            mClickedPoints.clear();
            mClickedPoints = new ArrayList<>();
        }

        /**  Adding current location to the ArrayList */
        mClickedPoints.add(start);
        Log.i("current location", start.toString());

        MarkerOptions options = new MarkerOptions();
        options.position(start);

        /** Destination click */
        mClickedPoints.add(point);

        /** Setting the position of the marker */
        options.position(point);

        /**  Checks if start and end locations are captured */
        if (mClickedPoints.size() >= 2) {
            orig = mClickedPoints.get(0);
            dest = mClickedPoints.get(1);
        }
        mMap.addMarker(options);
        request_directions_and_get_response();
    }
});

onLocationChanged()重写:

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    /** Setting current location marker */
    start = new LatLng(location.getLatitude(),location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(start);
    markerOptions.title("Current Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mCurrLocationMarker = mMap.addMarker(markerOptions);
}

只需将类似的代码放入onLocationChanged()覆盖中,以便在用户位置更改时更新它。 - Daniel Nugent
@Daniel 我已经这样做了,位置在移动,这很好。当我点击地图时,折线被绘制出来,蓝点在上面移动。我想一直更新折线。你知道有什么解决方案吗? - Mili Maric
展示你的 onLocationChanged 代码。 - Daniel Nugent
1个回答

1
为了使用当前的 clicked points ArrayList,您需要删除先前的原点,然后将当前原点添加到 ArrayList 的开头,然后根据新的当前位置重新计算方向。
类似这样:
@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    /** Setting current location marker */
    start = new LatLng(location.getLatitude(),location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(start);
    markerOptions.title("Current Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mCurrLocationMarker = mMap.addMarker(markerOptions);

    //If there is a previous route drawn:
    if (mClickedPoints.size() >= 2) {
        //remove the old origin:
        mClickedPoints.remove(0);

        //add the new one at the 0th position:
        mClickedPoints.add(0, start);

        //set orig and dest for directions:
        orig = mClickedPoints.get(0);
        dest = mClickedPoints.get(1);

        //get updated directions:
        request_directions_and_get_response();
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接