简体   繁体   English

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

[英]Android google maps marker constantly clicking on the same point

When I click somewhere on map, app draw polyline between current location and destination (click). 当我点击地图上的某个地方时,app会在当前位置和目的地之间绘制折线(点击)。 I want that destination marker is clicking by himself all the time (every few seconds), so on user movement new polylines are drawing (updating the last one). 我希望目标标记始终单击(每隔几秒),因此在用户移动时,新的折线正在绘制(更新最后一个)。

I tried with handler and bunch of the things and didn't find solution. 我尝试使用处理程序和一堆东西,但没有找到解决方案。

Any answer would be highly appreciated. 任何答案都将受到高度赞赏。

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

In order to use the clicked points ArrayList as you have it now, you'll need to remove the previous origin, then add the current origin to the start of the ArrayList, and then get re-calculated directions based on the new current location. 为了现在使用点击点ArrayList,您需要删除先前的原点,然后将当前原点添加到ArrayList的开头,然后根据新的当前位置重新计算方向。

Something like this: 像这样的东西:

@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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM