【问题标题】:Android google maps marker constantly clicking on the same pointAndroid谷歌地图标记不断点击同一个点
【发布时间】:2017-08-09 00:08:21
【问题描述】:

当我点击地图上的某处时,应用程序会在当前位置和目的地之间绘制折线(点击)。我希望目标标记一直在自己点击(每隔几秒),所以在用户移动时,新的折线正在绘制(更新最后一条)。

我尝试了处理程序和一堆东西,但没有找到解决方案。

任何答案将不胜感激。

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 我已经这样做了,位置正在移动,效果很好。当我单击地图时,会绘制折线并且蓝点在其上移动。我想一直更新折线。你知道有什么解决办法
  • 显示您的 onLocationChanged 代码

标签: java android google-maps marker


【解决方案1】:

为了像现在一样使用单击的点 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();
    }

}

【讨论】:

    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多