Skip to content
Advertisement

Here SDK – Change map location

This is the sample code from Here API. But the problem I have is that the initial location where the map is loaded is wrong. What I need is for the map to be displayed where you are.

What is the method that modifies the position of the map?

private MapViewLite mapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get a MapViewLite instance from the layout.
    mapView = findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
}

private void loadMapScene() {
    // Load a scene from the SDK to render the map with a map style.
    mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
        @Override
        public void onLoadScene(@Nullable MapScene.ErrorCode errorCode) {
            if (errorCode == null) {
                mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915));
                mapView.getCamera().setZoomLevel(14);
            } else {
                Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
            }
        }
    });
}

Answer

This is the piece of code that hard codes the position:

mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915))

Instead, set the coordinates to be the user’s click. In order to do so, you will need to surround that piece of code with an onTouch() listener and check the action of the user. I’ll use an ImageView as an example. So something like this:

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //whatever you want to do
            //to fetch the coordinates of the user's click use event.getX() and event.getY()
        }
        return true;
    }
});
Advertisement