How to search locations offline in here maps?

Viewed 103

Here the sample code for search feature which works only when data is on.

  private void searching() {

        Log.d(TAG, "init: initializing");
        search = (EditText) findViewById(R.id.input_search);
        search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == EditorInfo.IME_ACTION_SEARCH || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    Log.d(TAG, "geolocate: Geolocating");
                    Geocoder geocoder = new Geocoder(MainActivity.this);
                    List<Address> list = new ArrayList<>();
                    String searchString = search.getText().toString();
                    try {
                        list = geocoder.getFromLocationName(searchString, 1);
                    } catch (IOException e) {
                        Log.e(TAG, "geolocater: IO exception" + e.getMessage());
                    }
                    if (list.size() > 0) {
                        final Address address = list.get(0);
                        Log.d(TAG, "Found the address:" + address.toString());
                        GeoCoordinate geoCoordinate=new GeoCoordinate(address.getLatitude(),address.getLongitude());
                        Image image=new Image();
                        try {
                            image.setImageResource(R.drawable.marker1);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (mm != null) {
                            mm.setVisible(false);
                        }
                        mm=new MapMarker(geoCoordinate,image);
                       map.addMapObject(mm);
                        map.setCenter(geoCoordinate,
                                Map.Animation.BOW, 13.0d, 180, 45);
                        map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 1.5);
                    }
                }
                return false;
            }
        });
    }

But I thought it would work the same during offline but it isn't. I also have downloaded the map offline, but some how this code is not working on the downloaded map. how can i solve this

1 Answers

You can launch online or offline search without changing the device or HERE SDK connectivity by using setConnectivity(Connectivity) method on a Request instance.

To ensure that the connectivity mode is applied, call setConnectivity(Connectivity) before executing a Request.

If a Connectivity.ONLINE search request fails due to connection issues, HERE SDK returns the ErrorCode.UNKNOWN error code. If a Connectivity.ONLINE Geocoding or Reverse Geocoding request fails due to connection issues, HERE SDK returns the ErrorCode.NETWORK_COMMUNICATION error code. If a Connectivity.OFFLINE search request fails due to not enough cached data, HERE SDK returns with zero results. If you attempt to execute a TextAutoSuggestionRequest with the Connectivity.OFFLINE connectivity mode, HERE SDK returns the ErrorCode.SERVICE_UNAVAILABLE error code since auto suggestions are only supported online.

Please refer https://developer.here.com/documentation/android-premium/dev_guide/topics/places-offline.html

Also share the error response that you received upon running this code.

Related