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.
Related
I am trying to search place using GMSPlacesClient autocompleteQuery by keyword within a specified city or country. My Google Map account is Standard with enabled billing.
Code
GMSVisibleRegion visibleRegion = [GMSMapShareInstance sharedInstance].projection.visibleRegion;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:visibleRegion.nearLeft
coordinate:visibleRegion.nearRight];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterGeocode;
filter.country = #"US";
GMSPlacesClient *placesClient = [GMSPlacesClient sharedClient];
[placesClient autocompleteQuery:searchTerms
bounds:bounds
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(#"Search error: %#", [error localizedDescription]);
return;
}
arrSearchedResult = results;
[self.tblSearchResult reloadData];
}];
But it returns results that are seem not exactly. I don't know whether it's affected by account type (Standard, Premium) or not.
Anyone experienced a similar issue? Or have any way to improve search result?
Thanks in advance!
(Sorry for my English)
I'd like to add annotation manually (when user touch in to the particular place in map view) and to get the details of that location (latitude,longitude,address)..
as #iPrabu directed to similar post, with very good and correct answer for your problem... for second part i.e. to get details about that location..you can do something like this
-(void)getAddressFromCurruntLocation:(CLLocation *)location{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(placemarks && placemarks.count > 0)
{
CLPlacemark *placemark= [placemarks objectAtIndex:0];
NSString *address = [NSString stringWithFormat:#"%# %#",[placemark country],[placemark administrativeArea]];
// you may also get locality,sublocality,subadministrativeare etc
NSLog(#"The Address Is:%#",address);
}
}];
}
Happy Coding :)
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) {
}];
I am working on Google Places AutoComplete Service where i need to filter the places specific to country and found this.
It is working good untill when i pass a parameter components=country:se to filter the autocomplete field its response is REQUEST_DENIED
NSMutableString *url = [NSMutableString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%#&components=country:se&sensor=%#&key=%#",
[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
SPBooleanStringForBool(sensor), key];
I need the autocomplete to give suggestion specific to country.
Thanks
This works fine for me.
You can set this by adding the bounds for the region by adding its extreme top left and bottom right location points of the the Geographical area.
like for USA i used.
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(52.00, 148.00)
coordinate:CLLocationCoordinate2DMake(18.00, -71.00)];
GMSPlacesClient * placesClient = [[GMSPlacesClient alloc] init];
[placesClient autocompleteQuery:fieldValue bounds:bounds filter:nil
callback:^(NSArray *results, NSError *error)
{
if (error != nil)
{
NSLog(#"Autocomplete error %#", [error localizedDescription]);
return;
}
else
{
[self.autoComplete removeAllObjects];
[self.AddressPlaceID removeAllObjects];
for (GMSAutocompletePrediction* result in results)
{
CustomAddress * parseAddress = [[CustomAddress alloc] init];
NSString * adString = [parseAddress removeCountryFromAddress:result.attributedFullText.string];
[self.autoComplete addObject:adString];
[self.AddressPlaceID addObject:result.placeID];
}
[self.autoCompleteTable reloadData];
}
}];
But you can still get the other location results . It will prefer first the geographical bounds you define in the GMCoordinateBounds
Why do not use the iOS SDK to obtain country specific results? Follow this answer.
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;
}
}
}];
}