Geo/Reverse Geocoding
Geocoding
1. Import header files
#import <NavinfoKit/NavinfoKit.h>
2. Configure APIKEY
Refer to Project Configuration Instructions.
3. Define NavinfoGeocodeSearch
Define the geographic and inverse geocode search object NavinfoGeocodeSearch and inherit the search protocol <NavinfoGeocodeSearchDelegate >.
4. Construct NavinfoGeocodeSearch
Structure and reverse geocoding search objects NavinfoGeocodeSearch
_geocoder = [NavinfoGeocodeSearch new]; _geocoder.delegate = self;
5. Set up geocoding search query parameters
The request parameter class of the geocoding query is NavinfoGeocodeQuery, and the formatted address is a mandatory parameter. The rules follow: country, province, city, district, town, town, village, street, house number, housing, building. Such as: Beijing Dongzhimen South Street No. 10; cityName (city name) search administrative area adcode / name, the default 110000; other non-essential parameters, please refer to the API documentation.
NavinfoGeocodeQuery *query = [[NavinfoGeocodeQuery alloc] init]; query.formattedAddress = @"Tengfei Software Park Huixianyuan 1"; query.cityName = @"Dalian";
6. Initiate search query parameters
Initiate a search request by calling the geocodeWithQuery method of NavinfoGeocodeSearch.
[_geocoder geocodeWithQuery:query];
7. Processing data in callbacks
Proxy method when the query is successful.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch * _Nonnull)geocoder geocodeResult:(NavinfoGeocodeResult * _Nonnull)result;
where NavinfoGeocodeResult returns to get the query result at this time.
Description:
1) Get geo- and inverse geocoding instances via geocoder.
2) Get geocoded information via result.geoInfo.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch *)geocoder geocodeResult:(NavinfoGeocodeResult *)result { NSLog(@"----> geocoderesult: lonlat=%@, score=%f", [NavinfoLonlat toSringWithLonlats:@[result.geoInfo.lonlat]], result.geoInfo.score); }
8. Processing failed queries
When the retrieval fails, Error returns, and the reason for the failure is obtained by the callback function.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch * _Nonnull)geocoder error:(NSError * _Nonnull)error;
where Error returns, and the reason for the resulting failure is obtained by the callback function.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch *)geocoder error:(NSError *)error { NSLog(@"----> error"); }
Reverse Geocoding
1. Import header files
#import <NavinfoKit/NavinfoKit.h>
2. Configure APIKEY
Refer to Project Configuration Instructions.
3. Define NavinfoGeocodeSearch
Define the geographic and inverse geocode search object NavinfoGeocodeSearch and inherit the search protocol <NavinfoGeocodeSearchDelegate >.
4. Construct NavinfoGeocodeSearch
Construct a geographic and inverse geocoded search object NavinfoGeocodeSearch and set the proxy.
_geocoder = [NavinfoGeocodeSearch new]; _geocoder.delegate = self;
5. Set reverse geocoding search query parameters
The request parameter class of the geocoding query is NavinfoReGeocodeQuery, lonlats (longitude and latitude coordinate set) is a mandatory parameter, and the maximum support 10 sets of coordinates, the excess is not processed; requirePOI (whether to recall nearby POI information) Non-required parameters; requireRoad (whether to recall nearby road information) Non-required parameters; other non-essential parameters please refer to the API documentation.
NavinfoReGeocodeQuery *query = [NavinfoReGeocodeQuery new]; NavinfoLonlat *lonlat1 = [[NavinfoLonlat alloc] initWith:11648149 latitude:3999046 gbType: NavinfoGbTypeG02]; query.lonlats = @[lonlat1]; query.requirePOI = YES; query.requireRoad = YES;
6. Initiate search query parameters
Initiate a search request by calling the reverseGeocodeWithQuery method of NavinfoGeocodeSearch.
[_geocoder reverseGeocodeWithQuery:query];
7. Processing data in callbacks
Proxy method when the query is successful
- (void)onGeocodeSearch:(NavinfoGeocodeSearch * _Nonnull)geocoder geocodeResult:(NavinfoGeocodeResult * _Nonnull)result;
where NavinfoGeocodeResult returns to get the query result at this time.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch *)geocoder geocodeResult:(NavinfoGeocodeResult *)result { NSLog(@"----> geocoderesult: lonlat=%@, score=%f", [NavinfoLonlat toSringWithLonlats:@[result.geoInfo.lonlat]], result.geoInfo.score); }
Description:
1) Get geo- and inverse geocoding instances via geocoder.
2) Get latitude and longitude information via result.lonlat.
3) Get formatted address information via result.formattedAddress.
4) Get the NavinfoAddressInfo detail address collection via result.address.
5) Get the road information collection near NavinfoRoadInfo via result.roads.
6) Get the POI information collection near NavinfoPOI via result.pois.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch *)geocoder reGeocodeResult:(NavinfoReverseGeocodeResult *)result { NSLog(@"----> reGeocodeResult"); if (result.lonlat) { NSString *lonlatStr = result.lonlat.lonlatString; NSString *fAddress = result.formattedAddress; NSString *adCountry = result.address.country; NSString *adProvince = result.address.province; NSString *adCity = result.address.city; NSString *adAdcode = result.address.adcode; NSLog(@"----> reGeocodeResult: lonlat=%@, formattedAddress=%@, adCountry=%@, adProvince=%@, adCity=%@, adAdcode=%@", lonlatStr, fAddress, adCountry, adProvince, adCity, adAdcode); if (result.roads) { NSArray *roads = result.roads; for (NSInteger i = 0; i < [roads count]; i++) { NavinfoRoadInfo *roadInfo = roads[i]; NSString *roadName = roadInfo.name; NSString *distance = roadInfo.distance; NSString *direction = roadInfo.direction; NSString *lonlatSrt = roadInfo.lonlat.lonlatString; NSLog(@"----> reGeocodeResult.roadInfo: roadName=%@, distance=%@, direction=%@, lonlatSrt=%@", roadName, distance, direction, lonlatSrt); } } if (result.pois) { NSArray *pois = result.pois; for (NSInteger i = 0; i < [pois count]; i++) { NavinfoPOI *poiInfo = pois[i]; NSString *pId = poiInfo.poiId; NSString *name = poiInfo.name; NSString *type = poiInfo.type; NSString *distance = poiInfo.distance; NSString *direction = poiInfo.direction; NSString *address = poiInfo.address; NSString *lonlatStr = poiInfo.lonlat.lonlatString; NSLog(@"----> reGeocodeResult.poiInfo: pId=%@, name=%@, type=%@, distance=%@, direction=%@, address=%@, lonlatStr=%@", pId, name, type, distance, direction, address, lonlatStr); } } } }
8. Processing failed queries
When the retrieval fails, Error returns, and the reason for the failure is obtained by the callback function.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch * _Nonnull)geocoder error:(NSError * _Nonnull)error;
where Error returns, and the reason for the resulting failure is obtained by the callback function.
- (void)onGeocodeSearch:(NavinfoGeocodeSearch *)geocoder error:(NSError *)error { NSLog(@"----> error"); }