Xamarin.Forms如何在MapView上创建可拖动标记(Marker)?
Draggable Markers in Xamarin.Forms Maps: Getting Started
Hi there! Since you already have Android and Swift development experience, you’ll catch on to this fast. Here’s your starting direction for implementing draggable markers (since the out-of-the-box Xamarin.Forms.Map only supports basic Pins):
- Use Custom Renderers: This is the go-to way to tap into native platform features in Xamarin.Forms. You’ll build platform-specific renderers for your Map control to hook into the native Google Maps (Android) and MapKit (iOS) APIs you already know.
- For Android:
- Create a custom renderer that inherits from
MapRenderer - Access the native
GoogleMapinstance in the renderer - When creating native
Markers from your Forms Pins, setDraggable = true - Listen to native drag events (
MarkerDragStart,MarkerDrag,MarkerDragEnd) and pass these events back to a custom Xamarin.Forms Map subclass (you’ll need to define this subclass with drag event handlers)
- Create a custom renderer that inherits from
- For iOS:
- Build a custom renderer inheriting from
MapRenderer - Get the native
MKMapView - For the
MKAnnotationViewcorresponding to your Forms Pin, setDraggable = true - Implement the
MKMapViewDelegatemethods likeDidDragAnnotationViewto handle drag events and relay them to your Forms-level Map control
- Build a custom renderer inheriting from
- For Android:
- Optional: Custom DraggablePin Class: Define a subclass of
Pin(e.g.,DraggablePin) to flag which pins should be draggable. This makes it easier to handle only specific pins in your renderers instead of all pins.
内容的提问来源于stack exchange,提问作者TJBlack31
相关产品推荐
相关产品推荐

