简体   繁体   中英

Mapbox custom user location annotation image and position

I want to be able to display a custom image for the user location on a Mapbox map in iOS, and be able to set the current user location from an internal source, rather than using the internal GPS source.

The built-in user location facility appears to only be able to source location from the Mapbox internal location engine. I have added a custom annotation to the map, and I can update its position when I get a new location fix, but the motion is jerky (I can pan the map with a camera animation, but not the annotation.)

Is there any way in the iOS Mapbox SDK to animate changing the location of an annotation?

So this is what I came up with. In the MGLMapViewDelegate I use the didFinishRenderingFrame() callback to update the location, interpolating between the two most recent locations. posAnnotation is the MGLPointAnnotation associated with flightMarker which is an MGLAnnotationView extended to add a UIImageView and hold the locations.

        override fun didFinishRenderingFrame(mapView: MGLMapView, fullyRendered: Boolean) {
            val frac = min((CFDate.getCurrentAbsoluteTime() - lastUpdateTimestamp) / lastUpdateInterval, 1.0)
            if(flightMarker.location.valid)
                posAnnotation.coordinate = flightMarker.interpolatedLocation(frac)
        }

        override fun viewForAnnotation(mapView: MGLMapView, annotation: MGLAnnotation): MGLAnnotationView? {
            return if (annotation == posAnnotation) flightMarker else null
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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