简体   繁体   中英

Here Maps Android SDK position indicator rotation

I am using HERE MAPS PREMIUM SDK for my application. I need my current location icon to be rotated as the direction changes. Similar to Current location indicator in other maps. Since HERE SDK doesn't have default method to it, I am using following code to rotate Position Indicator whenever onPositionUpdated() callback gets called as below,

private PositioningManager.OnPositionChangedListener mapPositionHandler = new PositioningManager.OnPositionChangedListener() {
        @Override
        public void onPositionUpdated(PositioningManager.LocationMethod method, GeoPosition position,
                                      boolean isMapMatched) {                        
                        Matrix matrix = new Matrix();
                        matrix.postRotate((float) mHeading - mMap.getOrientation());
                        Bitmap newBitmap = Bitmap.createBitmap(myLocationIcon, 0, 0, myLocationIcon.getWidth(), myLocationIcon.getHeight(), matrix, true);
                        Image newImage = new Image();
                        newImage.setBitmap(newBitmap);
                        if (mMapFragment.getPositionIndicator() != null) {
                            mMapFragment.getPositionIndicator().setMarker(newImage);
                        }
}

This code works as expected but the position Doesn't updated smoothly. it's kind of bouncing whenever position updates. Any suggestion on this would be so helpful.

You do not need to set the icon on each Location Update.Just set the image icon only one time to the position indicator by calling method setMarker. If you need to change the icon then you can call setMarker again. Now as you need the location icon to be rotated as the direction changes,You can use the following code block inside onPositionUpdated

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onPositionUpdated(final PositioningManager.LocationMethod locationMethod,
                              final GeoPosition geoPosition, final boolean mapMatched) {
   
        final GeoCoordinate coordinate = geoPosition.getCoordinate();
        map.setCenter(coordinate, Map.Animation.BOW);
        map.setOrientation((long) Math.floor(geoPosition.getHeading() + 0.5d), Map.Animation.BOW);

}

Now you are also looking for PositionIndicator to be updated smoothly.So you can use method setSmoothPositionChange(true) when you are setting marker like the following:

            Image navigation_pointer = new Image();
            navigation_pointer.setImageResource(R.drawable.navigation_ico_new);
            mMapFragment.getPositionIndicator().setMarker(navigation_pointer);
            mMapFragment.getPositionIndicator().setSmoothPositionChange(true); 

Please Note: This method setSmoothPositionChange(true) is only available in the latest HERE MAP SDK which is 3.16 and this version of SDK uses Android X.

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