Search (v2.1.x)

In Navmii SDK it is possible to search addresses and places. You can get a NMSearchService instance via searchService readonly property in the NMSdk singleton. NMSearchService is a factory that creates the objects needed to perform all possible lookups in the SDK.

NMSearchService *searchService = [NMSdk sharedInstance].searchService;

All the classes used for search in the SDK inherit from NMRequest abstract class. So, all available requests have the following parameters:

  • search location
  • maximum number of search results
  • locale

These parameters can be set via searchLocation, maxResultsCount and locale properties. There is also status property showing the request's current status represented as NMRequestStatus enum:

  • NMRequestStatusNone
  • NMRequestStatusProceeding
  • NMRequestStatusSucceeded
  • NMRequestStatusCanceled
  • NMRequestStatusFailed

NMRequest has method startWithCompletion: which takes the NMRequestCompletion as an argument. NMRequestCompletion is a typedef:

typedef void(^NMRequestCompletion)(NMRequest *request, NSArray<NMPlace *> *results, NMRequestError *error);

So, you can pass a block where you handle the search results (NSArray of NMPlaces), error (NMRequestError) and the request itself. NMRequestError helps you to identify if you had launched the request before the SDK was initialized or the search was already in process. You also can cancel the request with the cancel method.

COUNTRY AND STATE SEARCH

The fastest and the most light-weighted request is NMCountryAndStateRequest. Use this request instead of NMReverseGeocodingRequest when you want to know only country and state at some coordinates. You can create an instance of NMCountryAndStateRequest via NMSearchService's createCountryAndStateRequestWithCoordinates: method. When you perform this search, an array containing a single NMPlace item will be returned in the completion block.


NMCountryAndStateRequest *request = [searchService createCountryAndStateRequestWithCoordinates:searchLocation];
[request startWithCompletion:^(NMRequest * _Nonnull request, NSArray * _Nonnull results, NMRequestError * _Nonnull error) {
	NMPlace *place = (NMPlace *)[results firstObject];
    NSLog(@"country - %@", place.address.country.name");
	NSLog(@"state - %@", place.address.state.name);
}];

REVERSE GEOCODING SEARCH

If you want to get the address at some coordinates use NMReverseGeocodingRequest. To create a NMReverseGeocodingRequest object use NMSearchService's createReverseGeocodingRequestWithCoordinates: method.

NMReverseGeocodingRequest *request = [searchService createReverseGeocodingRequestWithCoordinates:searchLocation];
[request startWithCompletion:^(NMRequest * _Nonnull request, NSArray * _Nonnull results, NMRequestError * _Nonnull error) { 
	NMPlace *place = (NMPlace *)[results firstObject];
	NSLog(@"address: %@, %@", place.address.street, place.address.houseNumber);
}];

Note: if only country and state are relevant for your search please refer to COUNTRY AND STATE SEARCH.

POI SEARCH

You can perform search for Places of Interest (POIs) located within a search radius from some coordinates and containing a specified text string in their names. In order to do this use a NMPoiRequest instance created by createPoiRequestWithSearchQuery:andCoordinates: method of NMSearchService. By default, the search is performed within the radius of 100 meters and among all POI categories. You can specify this parameters via poiCategories and searchRadius properties. Search results in the returned array will be sorted by distance (from closer to farther) from the search location.

//logs out names of all the POIs containing "bar" within 1000 meters radius from London's center.
NMMapCoordinates *mapCoordinates = [NMMapCoordinates mapCoordinatesWithLongitude:-0.12574
                                                                     andLatitude:51.50833];
NMPoiRequest *request = [searchService createPoiRequestWithSearchQuery:@"bar"
                                                        andCoordinates:mapCoordinates];
request.searchRadiusInMeters = 1000;
[request startWithCompletion:^(NMRequest * _Nonnull request, NSArray<NMPlace *> * _Nonnull results, NMRequestError * _Nonnull error) {
	for (NMPlace *place in results) {
		NSLog(@"place name - %@", place.name);
	}  
}];

GEOCODING SEARCH

You also can get coordinates for a NSString representing a particular address. In order to do this use a NMGeocodingRequest instance. A NMGeocodingRequest can be created using createGeocodingRequestWithSearchQuery:andCoordinates: method of NMSearchService.

//logs out coordinates of all found places containing "Piccadilly" in its address
NMMapCoordinates *mapCoordinates = [NMMapCoordinates mapCoordinatesWithLongitude:-0.12574
                                                                     andLatitude:51.50833];
NMPoiRequest *request = [searchService createGeocodingRequestWithSearchQuery:@"Piccadilly"
                                                              andCoordinates:mapCoordinates];
[request startWithCompletion:^(NMRequest * _Nonnull request, NSArray<NMPlace *> * _Nonnull results, NMRequestError * _Nonnull error) {

	for (NMPlace *place in results) {
		NSLog(@"coordinates: %f lat, %f lon", place.coordinates.latitude, place.coordinates.longitude);
	}
}];