MKLocalSearch returning results outside of region - ios

I'm using Apple Maps to get a list of addresses in the local area. However, it seems to be returning results from all over the world, rather than the map region I am specifying.
I am using the following code, and have checked the region to make sure it is 'broadly' the whole of London (see attachment) for mapView with the same parameters. However in my results I sometimes have locations in Germany, USA or South America.
Anyone can see what I'm doing wrong?
MKLocalSearchRequest* request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchTerm;
CLLocationCoordinate2D cornerCoordinate = CLLocationCoordinate2DMake(51.5007282, -0.1246263);
request.region = MKCoordinateRegionMakeWithDistance(cornerCoordinate, 50000, 50000);
MKLocalSearch* search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
//results come in here
}];
Map region:

According to the documentation:
Specifying a region does not guarantee that the results will all be
inside the region. It is merely a hint to the search engine.
It's bummer, but there's unfortunately no way to limit the results just to the provided region.
You may consider use a Google Places API for that: https://developers.google.com/places/webservice/autocomplete

Try this solution. Here I think issue is because of the region you specified.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchTerm;
MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1);
CLLocationCoordinate2D cornerCoordinate = CLLocationCoordinate2DMake(51.5007282, -0.1246263);
request.region = MKCoordinateRegionMake(cornerCoordinate, span);
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
}];

Related

Apple CLGeocoder,MKLocalSearch result differ from device map result

In my application search with the location string, it ll return the 5 result, but that not match with default device map app.
my code 1 : CLGeocoder
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:searchKey completionHandler:^(NSArray* placemarks, NSError* error){
dispatch_async(dispatch_get_main_queue(), ^{
if (placemarks.count > 0) {
searchLocations = placemarks;
} else {
searchLocations = nil;
}
[searchTableView reloadData];
for (CLPlacemark *placemark in placemarks) {
NSLog(#"place dic %#",placemark.addressDictionary);
}
});
}];
Code 2 : MKLocalSearch
CLLocation *userLoc = (CLLocation *)[[MYLocationManager defaultManager] currentLocation];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = MKCoordinateRegionMakeWithDistance(userLoc.coordinate, 100000, 100000);
request.naturalLanguageQuery = searchKey;
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
NSMutableArray *locs = [[NSMutableArray alloc]init];
for (MKMapItem *placeM in response.mapItems) {
[locs addObject:placeM.placemark];
}
if (locs.count > 0) {
searchLocations = locs;
} else {
searchLocations = nil;
}
dispatch_async(dispatch_get_main_queue(), ^{
[searchTableView reloadData];
});
}];
Both are return a same result
Device map app result :
Device map result differ from coding geo results. please help to solve this.
and my question is what type of search methodology use the default map application?
and how to get same result in coding ?
Please refer this answer. It describe all the steps needed to replicate the above result. I've tested this on simulator.
The result of the query depends on the region you're searching in.
That's the 'Local' in MKLocalSearch.
The location in your code example is the user's location in a region with a side length on 10km.
The region where the built in map searches should be equal to or dependant on the region you're showing in the App at this very moment.
The region in Apples App is probably very different from the explicit region in your App.

MKLocalSearch not working like Apple Maps search?

I have created a MapKit and trying to play around with MKLocalSearch. One thing I noticed in comparison to Apple Maps, is that mklocalsearch is restricted to 10 results. So how does Apple Maps display 15 suggestions under the search bar?
Okay, on to an example. Im trying to find "Barcelona." In Apple Maps it will be suggested after writing just "barc" and it will stay on the suggestion list throughout typing barcelona.
Now in my own Map view, I actually have to type in the full Barcelona to get the suggestion: Spain, Barcelona. On my way I get other suggestions, but nothing like Spain, Barcelona and not like Apple maps.
Any insight on how to get it working and to why Apple Maps work differently (spec. the 15 results vs 10 with mklocalseach)
Here is the code called on textField Changes:
- (IBAction)searchFieldChanged:(UITextField *)sender {
if(self.locationTextfield.text.length>0)
self.tableView.hidden = NO;
else
self.tableView.hidden = YES;
NSString *query = self.locationTextfield.text;
// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = query;
request.region = self.mapsView.region;//we dont want region-specific search results!
//request.region = MKCoordinateRegionMakeWithDistance(self.mapsView.userLocation.location.coordinate,40000000, 15000000);
// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
[placeMarks removeAllObjects];
NSLog(#"p-count: %lu", response.mapItems.count);
for (MKMapItem *item in response.mapItems) {
[placeMarks addObject:item.placemark];
self.tempPlacemark = item.placemark;
NSLog(#"placemark: %#", item.placemark);//.location.coordinate.latitude);
}
//if(placemarks.count==0)
// appDelegate.staticPlacemark = nil;
//[self.mapsView removeAnnotations:[self.mapsView annotations]];
//[self.mapsView showAnnotations:placemarks animated:NO];
[self.tableView reloadData];
}];
}
What you do is a MKLocalSearch using a MKLocalSearchRequest. What Apple in its macOS and iOS map apps does is using the newer MKLocalSearchCompleter class to obtain autocompletion suggestions. These suggestions are used for realtime search and displayed in a UITableView. When the user selects one entry that suggestion is used to initialize a MKLocalSearchRequest to obtain detailled information about this location.

Map Location names in drop down list

My App Has to integrate search location names using text box. Is Map kit has any method to implement this kind of a functionality ? This below image displays what i exactly need
No , there is not any method of MapKit to implement this type of property..
For That You have to implement Your own Code, i.e. use of UISearchbarCantroller or Simple UISearchbar.
For Search results you can use Google Place Autocomplete api.
You can Use this GooglePlacesAutocomplete Example.
In
iOS >= 6.1 provides MKLocalSearch, MKLocalSearchRequest to search for natural language points of interest. Sample
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = regionToSearchIn;
request.naturalLanguageQuery = #"restaurants"; // or business name
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
// do something with the results / error
}];

What is proper way to get ETA (estimated time arrival) from any location to my current location

Would like to know what is proper way to get ETA (estimated time arrival) from any location to my current location, in consideration the following situation:
a. ex. - I got from another device its location (lon/lat) and want to when the other person will pick me up... In this case what web-service can I use to get this info for the user?
Does mapkit provides that kind of option?
b. In case it will be done on the server-side and I'll just send my user location, what are the tools my server-side programmer can use to get ETA info in order to send it back to my user?
Thank you all in advance.
I saw this: Is there any way to determine the driving time between two locations using Apple's Maps API?
- the problem, as I found in other places, (to my understanding)is that google api requires use of Google Maps app that isn't installed on every iOS user now.
I know this post is a bit old but in case someone is looking at the answer since iOS 7 Apple provide an API in MapKit in order to calculate all these info.
Here is a snippet of how to use this API
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
[request setDestination:destination];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
[request setRequestsAlternateRoutes:NO];
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if ( ! error && [response routes] > 0) {
MKRoute *route = [[response routes] objectAtIndex:0];
//route.distance = The distance
//route.expectedTravelTime = The ETA
}
}];
This worked for me using route.distance from M to the K's answer
I was modifying code from this tutorial map directions tutorial
(IBAction)routeButtonPressed:(UIBarButtonItem *)sender {
MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:thePlacemark];
[directionsRequest setSource:[MKMapItem mapItemForCurrentLocation]];
[directionsRequest setDestination:[[MKMapItem alloc] initWithPlacemark:placemark]];
directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
if (error) {
NSLog(#"Error %#", error.description);
} else {
routeDetails = response.routes.lastObject;
[self.mapView addOverlay:routeDetails.polyline];
self.destinationLabel.text = [placemark.addressDictionary objectForKey:#"Street"];
self.distanceLabel.text = [NSString stringWithFormat:#"%0.1f Miles", routeDetails.distance/1609.344];
self.etaLabel.text = [NSString stringWithFormat:#"%0.1f minutes",routeDetails.expectedTravelTime/60];
//self.transportLabel.text = [NSString stringWithFormat:#"%u" ,routeDetails.transportType];
self.allSteps = #"";
for (int i = 0; i < routeDetails.steps.count; i++) {
MKRouteStep *step = [routeDetails.steps objectAtIndex:i];
NSString *newStep = step.instructions;
self.allSteps = [self.allSteps stringByAppendingString:newStep];
self.allSteps = [self.allSteps stringByAppendingString:#"\n\n"];
self.steps.text = self.allSteps;
}
}
}];
}

Is it possible to use MKLocalSearch to find all nearby businesses?

In order to avoid the API limits of foursquare or one of the other local search providers, I would like to use MKLocalSearch from iOS 6.1. The following code:
MKLocalSearchRequest *localSearchRequest = [[MKLocalSearchRequest alloc] init];
MKCoordinateRegion localSearchRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake([theLocationChange.latitude floatValue], [theLocationChange.longitude floatValue]), 500.0f, 500.0f);
localSearchRequest.naturalLanguageQuery = #"restaurants";
localSearchRequest.region = localSearchRegion;
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:localSearchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
if (error)
{
NSLog([error localizedDescription]);
}
for (MKMapItem* mapItem in response.mapItems)
{
NSLog(#"mapitem name is: %#",mapItem.name);
}
}];
will correctly fetch and display restaurants near the specified location. If I change localSearchRequest.naturalLanguageQuery to "hotels", it will fetch and display hotels. The same applies for "hospitals", "bars", etc. If, however, I try an empty string, or " ", "*", or "?" for localSearchRequest.naturalLanguageQuery, it returns no results.
If I use the foursquare API and send it a location, I can easily get back a list of venues that includes local businesses of all types. Is there a way to use MKLocalSearch to return all venues or local businesses?
Apple documentation says that localSearchRequest.naturalLanguageQuery to be considered as natural Language query which means, you can type in anything and get the related results to your query. But Apples Maps database may not be returning the values as you expect.

Resources