In my project, I need to check user coordinates. If user lives in US, then I will enable a feature which shows air quality. Then further if user lives in Houston and surrounding area, then I will enable another feature which shows another feature, Ozone value. How could I achieve that? I could detect Houston and surrounding area with the following code, but I do not know how to find apart US states from Houston and World.
if((currentLocation.latitude>=28.930661 && currentLocation.latitude<=30.443953 && currentLocation.longitude>=-95.899668 && currentLocation.longitude<=-94.690486) ||
currentLocation.longitude == 0 || currentLocation.latitude == 0) {
// It is in the bounds
[self.opponentAvatarImageView removeFromSuperview];
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:#"Would you like to see ozone cloud in your area or a trace of your walking/driving/biking activity" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"OzoneMap", #"TraceMap", nil];
[actionsheet showInView:self.view];
}
Use the CLGeocoder class to pass in your location (CLLocation) and have it reverse geocode an address for you. You could even get specific with the City as well.
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:
^(NSArray* placemarks, NSError* error){
CLPlacemark *placemark = [placemarks firstObject];
if ([placemark.country isEqualToString:#"United States"]) {
// You're in the U.S.
}
}];
Related
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 :)
IOS newb just learning Mapkit. I load a map in my app using MKPlacemark. However some users may want to use more advanced features such as driving directions and for this, I think, they would be better off launching the native app on top of mine (with my app still open in the background when they finish with the regular map app)
I know how to launch the native app from my app using MKMapItem. However, s there a way to launch the native app only after the user touches the place mark.
Here is code I am using.
-(void) geoCodeAndMapIt {
NSString* location = #"156 University Ave, Palo Alto, CA 94301";
NSLog(#"going to map this address: %#",location);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithPlacemark:topResult];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);//5000 is meters
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
// The following MKMapItem class launches the full blown native app. Commenting it out causes the map to load in the app. Otherwise, it fires up the native map app immediately in place of the previous app.
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placemark];
mapItem.name = self.contact.first;
mapItem.phoneNumber = self.contact.tel;
NSDictionary *options = #{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumber numberWithInteger:MKMapTypeSatellite],
MKLaunchOptionsShowsTrafficKey:#YES
};
[mapItem setName:#"Name of your location"];
[mapItem openInMapsWithLaunchOptions:options];*/
}
}
];
[mapItem openInMapsWithLaunchOptions:options];
}
Thanks for any suggestions.
You should call the openInMaps only when the MKMapViewDelegate is called on didSelectAnnotation: for ex.
https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/index.html#//apple_ref/occ/intf/MKMapViewDelegate
To open the Maps app you could also build the URL yourself with the following:
UIApplication.sharedApplication().openURL(...)
Check this documentation here for the rest:
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
I have a MKMapView and want to grab the cities name based on the center of the MKMapView. I do not want to drop any pins or annotations at all. Just automatically grab the data based on center of MKMapView location. I know there is this method '[placemarkName locality]'. But is there a way without using a placemark?
You could just do a reverseGeocodeLocation (effective iOS 5+) from the centerCoordinate of the map view. For example, I could do something like:
CLLocationCoordinate2D center = self.mapView.centerCoordinate;
CLGeocoder *coder = [[CLGeocoder alloc] init];
[coder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude] completionHandler:^(NSArray *placemarks, NSError *error) {
self.cityLabel.text = [[placemarks firstObject] locality];
}];
This admittedly, returns an array of placemarks, but you don't have to do anything with them after you extract the city's name.
Just trying to determine a user's location. Here's the code:
self.locationManager = [[CLLocationManager alloc] init];
//self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];
self.geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: self.locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:#"Location" message:[NSString stringWithFormat:#"The app has determined that your country is: %# (%#)", placemark.country, placemark.ISOcountryCode] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}];
The first time I run the app, I get a double alert popup. The locationAlert pops up first, informing the user of a (null) country, then the default popup that says the app would like to use my location pops up pretty much simultaneously. If I click YES, the next time I run the app I will get the single popup of the user's correct country.
So my question is, how can I first gain permission to access the user's location, THEN run this code to find their location, without getting a null response because I've queried their location without permission?
The reverse geolocation code is trying to use the location property from the locationManager but it is not valid until the CLLocationManager delegate method locationManager:didUpdateToLocation:fromLocation: is called.
You should have a delegate method like this one to handle location updates.
By default on a map view we can show the user's location. On tapping the annotation pin, it will show "Current Location". But I want to show the address of the user as street, city and country. How can I do it by using CLGeoCoder class?
Do something like this:
CLGeocoder *gc = [[[CLGeocoder alloc] init] autorelease];
[gc reverseGeocodeLocation:locationObject completionHandler:^(NSArray *placemark, NSError *error) {
CLPlacemark *pm = [placemark objectAtIndex:0];
NSDictionary *address = pm.addressDictionary;
// do something with the address, see keys in the remark below
}];
And the relevant docs here:
http://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html#//apple_ref/occ/instp/CLPlacemark/addressDictionary