大佬教程收集整理的这篇文章主要介绍了Mapbox Android SDK – 可拖动标记,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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上的触摸事件上添加侦听器,并检查您的标记(或标记集合中的许多标记之一)是否受事件影响.
@H_190_11@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); } });以上是大佬教程为你收集整理的Mapbox Android SDK – 可拖动标记全部内容,希望文章能够帮你解决Mapbox Android SDK – 可拖动标记所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。