Skip to content

Search

This section explains how to implement online or offline geocoding and search into your Android application.

Autocomplete, Geocoding and SearchPlaces

Applications developed with the Sygic Maps SDK can use geocoding to transform textual query into geographical information. With the SearchManager you can perform three basic actions : Geocoding, ReverseGeocoding and Autocomplete. Please note that if you use the online services, you might want to define to send a request once every 300ms or so, otherwise some requests might end up with an error TOO_MANY_REQUESTS.

The autocomplete engine works with few simple steps:

The search has SESSIONS (offline, online and hybrid):

A session has a limited lifetime. The lifetime follows this call graph:

[0..N] autocomplete requests
then either of
[1] do nothing -> close session
[1] geocode with location ID -> close session
[1] geocode with query -> close session
[1] search places with query -> [0..N] search places with continuation token -> close session

After finding the desired result in autocomplete, you can take its locationID and call Geocode with it to get more precise information about the location. If you do not need to use autocomplete and have the full string that you want to search for, just use Geocode straight away.

When searching for places, you can create a new placeRequest and use your position, the categories that you want to search for and several other arguments.

Simple examples:

ArrayList<String> categories = new ArrayList<>();
categories.add("SYTouristInformationOffice");
categories.add("SYMuseum");

GeoCoordinates position = new GeoCoordinates(51.510175, -0.122445);
PlaceRequest placeRequest = new PlaceRequest(position, categories, /*radius*/ 1000);
SearchRequest searchRequest = new SearchRequest("London Eye", new GeoCoordinates(51.510175, -0.122445));
Session session = search.newHybridSession(); // search was received via the SearchManagerProvider

session.autocomplete(searchRequest, new AutocompleteResultListener() {
    @Override
    public void onAutocomplete(@NotNull List<? extends AutocompleteResult> list) {
    }

    @Override
    public void onAutocompleteError(@NotNull ResultStatus resultStatus) {
    }
});
session.geocode(searchRequest, new GeocodingResultsListener() {
    @Override
    public void onGeocodingResults(@NotNull List<? extends GeocodingResult> list) {
    }

    @Override
    public void onGeocodingResultsError(@NotNull ResultStatus resultStatus) {
    }
});
session.searchPlaces(placeRequest, new PlacesListener() {
     @Override
     public void onPlacesLoaded(@NotNull List<Place> list, @Nullable String s) {
     }

     @Override
     public void onPlacesError(@NotNull ResultStatus resultStatus) {
     }
});

Reverse geocoding

Reverse geocoding is the opposite process to geocoding, meaning you query by geographical location and receive textual address of the location. You can define a filter if you do not want to take walkways into account or pass it an emptySet.

ReverseGeocoderProvider.getInstance(new CoreInitCallback<ReverseGeocoder>() {
    @Override
    public void onInstance(@NonNull ReverseGeocoder reverseGeocoder) {
        reverseGeocoder.reverseGeocode(position, Collections.emptySet(), new ReverseGeocoder.ReverseGeocodingResultListener() { // do not forget to filter out the walkways if you do not want them
            @Override
            public void onReverseGeocodingResult(@NotNull List<? extends ReverseGeocodingResult> list) {
            }

            @Override
            public void onReverseGeocodingResultError(@NotNull ReverseGeocoder.ErrorCode errorCode) {
            }
        });
    }

    @Override
    public void onError(CoreInitException e) {
    }
});

During the implementation you will probably stumble upon a task of navigating to a search result. Let's see a simple example of how this would be achieved. Please bear in mind that in this example we do it in a simple callback succession and it's probably not what would be desired to exist in a complete application.

val search: com.sygic.sdk.search.SearchManager = SearchManagerProvider.getInstance().get() // make sure that it's not the Android's Search Manager
val searchRequest = SearchRequest("champs-elysees 121", GeoCoordinates(48.2879167, 17.2636084))
val searchSession = search.newOnlineSession()
val router = RouterProvider.getInstance().get()

searchSession.autocomplete(searchRequest, object : AutocompleteResultListener {
  override fun onAutocomplete(autocompleteResult: List<AutocompleteResult>) {
    // here you can show the list of the results to the user
    // the results contain info such as distance to the result, its type, or which parts of the text were matched
    // let's suppose that the user chooses the first result (usually the most relevant one)
    // now let's take the locationID and use it for calling the geocode() function
    searchSession.geocode(GeocodeLocationRequest(autocompleteResult[0].locationId), object : GeocodingResultListener {
        override fun onGeocodingResult(geocodingResult: GeocodingResult) {
            // this is where you get detailed information about the result
            // you also get the GeoCoordinates of the object
            val routePlan = RoutePlan() // create a route plan
            routePlan.setStart(your-last-known-location) // set the start point
            routePlan.setDestination(geocodingResult.location) // set the destination point - using the GeocodingResult
            router.computeRoute(routePlan, object: Router.RouteComputeListener{}) // compute the route. more in the Routing section
        }

        override fun onGeocodingResultError(status: ResultStatus) {
          // handle the errors here
        }
    })
  }

  override fun onAutocompleteError(status: ResultStatus) {
    // handle the errors here
  }
})