Change the transformCenter dynamically

我只是一个虾纸丫 提交于 2019-12-24 07:57:03

问题


I'm using HERE Android SDK Premium 3.9. My objective is this:

  1. Start off by showing the current location of the user at a certain PointF on the screen
  2. Based on some conditions, I want to add a "destination marker" on the screen. The destination marker is at a different location than the current location. This marker must not be at the center - rather it must be offset by a certain PointF

I haven't been able to achieve this. I can use setTransformCenter once for the initial "current location" and it takes effect. However if I call it again, it doesn't seem to take effect. My marker that I added in step 2 is just never at the correct position.

Here's some pseudocode:

hereMap.setTransformCenter(positionIndicatorPoint);

GeoCoordinate markerCoordinate = getMarkerCoordinates();
MapMarker marker = new MapMarker();
marker.setCoordinate(markerCoordinate);


hereMap.setTransformCenter(mapMarkerPoint);
hereMap.addMapObject(marker);
hereMap.setCenter(marker.getCoordinate(), Map.Animation.NONE);

What am I doing wrong?

EDIT:

It is not necessary for the current position indicator to be "on-screen" when the marker is displayed.


回答1:


  1. Make sure you call Map.setTransformCenter(PointF) with valid PointF. X and Y should be within [x from 0 to map.getWidth(), y from 0 to map.getHeight()] range.
  2. Method Map.setTransformCenter is only a setter, you need to make another method call like Map.setCenter() or Map.zoomTo(). to see how it works.

In your pseudocode in the second part you use hereMap.setCenter(marker.getCoordinate(), Map.Animation.NONE);, make sure you actually navigate to the new geo coordinate, not to the old one. But actually it should navigate to the new map center even if you re-navigate to the old geo coordinate.

I made small gist to show how Map.setTransformCenter(PointF) can be used.




回答2:


You can refer to our github repo for examples on MapMarker - https://github.com/heremaps/here-android-sdk-examples/blob/master/map-objects/app/src/main/java/com/here/android/example/map/objects/MapFragmentView.java. Below is the code snippet taken from github. You can look at the numerous other examples too. Hope this helps!

 /**
     * Initialize Create MapMarker Button to add/remove MapMarker.
     */
    private void initCreateMapMarkerButton() {
        m_marker_button = (Button) m_activity.findViewById(R.id.marker_button);

        m_marker_button.setOnClickListener(new View.OnClickListener() {
            // if MapMarker already exist on map, then remove MapMarker, other create MapMarker.
            @Override
            public void onClick(View v) {
                if (m_map != null && m_map_marker != null) {
                    m_map.removeMapObject(m_map_marker);
                    m_map_marker = null;
                } else {
                    createMapMarker();
                }
            }
        });
    }

    /**
     * create a MapMarker and add the MapMarker to active map view.
     */
    private void createMapMarker() {
        // create an image from cafe.png.
        Image marker_img = new Image();
        try {
            marker_img.setImageResource(R.drawable.cafe);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // create a MapMarker centered at current location with png image.
        m_map_marker = new MapMarker(m_map.getCenter(), marker_img);
        // add a MapMarker to current active map.
        m_map.addMapObject(m_map_marker);
}


来源:https://stackoverflow.com/questions/54513961/change-the-transformcenter-dynamically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!