gpt4 book ai didi

android - Mapbox Android SDK - 可拖动标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:58:14 32 4
gpt4 key购买 nike

如何在 Mapbox Android SDK 中使标记可拖动?这可能吗?

如果没有,还有哪些免费的开放 map 引擎支持此功能?

谢谢!

最佳答案

我必须为我的一个项目实现可拖动标记。我找到的唯一解决方案是扩展现有的标记类,方法是检查拖动事件并相应地更新标记位置。

package com.example.map;

import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.mapbox.mapboxsdk.api.ILatLng;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.overlay.Marker;
import com.mapbox.mapboxsdk.views.MapView;
import com.mapbox.mapboxsdk.views.util.Projection;

public class DraggableMarker extends Marker {

private static final String TAG = "map.DraggableMarker";

private boolean mIsDragged;
private static final RectF mTempRect = new RectF();
private static final PointF mTempPoint = new PointF();
private float mDx, mDy;

public DraggableMarker(String title, String description, LatLng latLng) {
super(title, description, latLng);
mIsDragged = false;
}

public DraggableMarker(MapView mv, String aTitle, String aDescription, LatLng aLatLng) {
super(mv, aTitle, aDescription, aLatLng);
mIsDragged = false;
}

public boolean drag(View v, MotionEvent event) {
final int action = event.getActionMasked();
if(action == MotionEvent.ACTION_DOWN) {
Projection pj = ((MapView)v).getProjection();
RectF bound = getDrawingBounds(pj, mTempRect);
if(bound.contains(event.getX(), event.getY())) {
mIsDragged = true;
PointF p = getPositionOnScreen(pj, mTempPoint);
mDx = p.x - event.getX();
mDy = p.y - event.getY();
}
}
if(mIsDragged) {
if((action == MotionEvent.ACTION_CANCEL) ||
(action == MotionEvent.ACTION_UP)) {
mIsDragged = false;
} else {
Projection pj = ((MapView)v).getProjection();
ILatLng pos = pj.fromPixels(event.getX() + mDx, event.getY() + mDy);
setPoint(new LatLng(pos.getLatitude(), pos.getLongitude()));
}
}

return mIsDragged;
}
}

稍后您需要在 MapView 上添加触摸事件监听器并检查您的标记(或标记集合中的许多标记之一)是否受该事件影响。

mMarker = new DraggableMarker(mMapView, "", "", aCenter);
mMarker.setIcon(new Icon(getActivity().getApplicationContext(), Icon.Size.SMALL, "marker-stroked", "FF0000"));
mMapView.addMarker(mMarker);

mMapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mMarker.drag(v, event);
}
});

关于android - Mapbox Android SDK - 可拖动标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793732/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com