IOS: reverse geocodign province - ios

I have this code to reverse geocoding and it work
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
MKPlacemark * myPlacemark = placemark;
// with the placemark you can now retrieve the city name
NSString *region = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStateKey];
NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
NSString *address = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStreetKey];
NSLog(#"region:%#", region);
NSLog(#"city:%#", city);
NSLog(#"address:%#", address);
}
It work fine but I'm not able to have "province"...what's the way to obtain province?

These are all the keys for the address dictionary:
const ABPropertyID kABPersonAddressProperty;
const CFStringRef kABPersonAddressStreetKey;
const CFStringRef kABPersonAddressCityKey;
const CFStringRef kABPersonAddressStateKey;
const CFStringRef kABPersonAddressZIPKey;
const CFStringRef kABPersonAddressCountryKey;
const CFStringRef kABPersonAddressCountryCodeKey;
Constants
kABPersonAddressProperty Identifier for the address multivalue property. Available in iOS 2.0 and later.
Declared in ABPerson.h.
kABPersonAddressStreetKey Street. Available in iOS 2.0 and later. Declared in ABPerson.h.
kABPersonAddressCityKey City. Available in iOS 2.0 and later. Declared in ABPerson.h.
kABPersonAddressStateKey State. Available in iOS 2.0 and later. Declared in ABPerson.h.
kABPersonAddressZIPKey Zip code. Available in iOS 2.0 and later. Declared in ABPerson.h.
kABPersonAddressCountryKey Country. Available in iOS 2.0 and later. Declared in ABPerson.h.
kABPersonAddressCountryCodeKey Country code. The supported values are listed in “Country Codes.” Available in iOS 2.0 and
later. Declared in ABPerson.h.
Unfortunately there isn't one for province.

In My code, I have following ways to get address and place names.
Province is State or administrativeArea
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count){
NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary];
addressLabel.Text = [dictionary valueForKey:#"Street"];
cityLabel.Text = [dictionary valueForKey:#"City"];
stateLabel.Text = [dictionary valueForKey:#"State"];
zipCodeLabel.Text = [dictionary valueForKey:#"ZIP"];
countryLabel.text = [dictionary valueForKey:#"Country"];
countryCodeLabel.text = [dictionary valueForKey:#"CountryCode"];
placeNameLabel.text = [placemarks[0] name];
addressNumberLabel.text = [placemarks[0] subThoroughfare];
addressLabel.text = [placemarks[0] thoroughfare];
neighborhoodLabel.text = [placemarks[0] subLocality];
cityLabel.text = [placemarks[0] locality];
countyLabel.text = [placemarks[0] subAdministrativeArea];
stateLabel.text = [placemarks[0] administrativeArea];
zipCodeLabel.text = [placemarks[0] postalCode];
countryLabel.text = [placemarks[0] country];
countryCodeLabel.text = [placemarks[0] ISOcountryCode];
inlandWaterLabel.text = [placemarks[0] inlandWater];
oceanLabel.text = [placemarks[0] ocean];
}
}];
You can try my open source project. It is easy to get place marks. Device-Details https://github.com/robert-yi-jones/Device-Details

Related

Show Location string using latitude and longitude

I am using this method to show the string of location using current location latitude and longitude but it is showing differently
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",location.coordinate.latitude, location.coordinate.longitude];
NSError* error;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
NSData *data = [locationString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dic = [[json objectForKey:#"results"] objectAtIndex:0];
NSArray* arr = [dic objectForKey:#"address_components"];
//Iterate each result of address components - find locality and country
NSString *cityName;
NSString *countryName;
for (NSDictionary* d in arr)
{
NSArray* typesArr = [d objectForKey:#"types"];
NSString* firstType = [typesArr objectAtIndex:0];
if([firstType isEqualToString:#"locality"])
cityName = [d objectForKey:#"long_name"];
if([firstType isEqualToString:#"country"])
countryName = [d objectForKey:#"long_name"];
}
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
but final location is showing this type :-
Final Location नठदिलà¥à¤²à¥,India
Why it is showing this type? Can anyone know about this.
Please supply the language with the API params. If language is not supplied, the geocoder attempts to use the preferred language as specified in the Accept-Language header, or the native language of the domain from which the request is sent.
So please replace the code as with the language parameter as like this.
NSString *urlString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false&language=en",location.coordinate.latitude, location.coordinate.longitude];
and try again.
I believe that is an uninitialzed variable which is pointing into random memory.
Try:
NSString *cityName = nil;
NSString *countryName = nil;
Short-circuit your for loop:
for (NSDictionary* d in arr)
{
// Add this after the existing code:
if (cityName && countryName)
break;
}
and check for errors before presenting the results:
if (cityName && countryName) {
NSString* locationFinal = [NSString stringWithFormat:#"%#,%#",cityName,countryName];
NSLog(#"Final Location %# ",locationFinal);
} else {
NSLog(#"Failed to find location");
}
Finally your JSON-processing code does no error-checking at all. That's a mistake.

How to get Mobile Number from vCard String Objective C

I am working on Action Extension Objective C. I have successfully created Extension for share recent contact in my Extension. In that I am getting v Card String. How can I get Mobile Number from v Card String. Any help would be appreciated.
Using contactsWithData:error: class method of CNContactVCardSerialization, you can retrieve info from a vCard.
It's from Contacts.framework, available since iOS9.
For earlier version, you can use AddressBook.framework. You can read info here.
NSError *errorVCF;
NSArray *allContacts = [CNContactVCardSerialization contactsWithData:[contactStr dataUsingEncoding:NSUTF8StringEncoding] error:&errorVCF];
if (!errorVCF)
{
NSMutableString *results = [[NSMutableString alloc] init];
//NSLog(#"AllContacts: %#", allContacts);
for (CNContact *aContact in allContacts)
{
NSArray *phonesNumbers = [aContact phoneNumbers];
for (CNLabeledValue *aValue in phonesNumbers)
{
CNPhoneNumber *phoneNumber = [aValue value];
[results appendFormat:#"%# %#\n", [aValue label], [phoneNumber stringValue]];
}
}
NSLog(#"Final: %#", results);
}

Loading Apple Pay Shipping Address No Street

I'm trying to get a shipping address extracted from the ABRecordRef provided by Apple. I have the following but my street is always returning as nil:
ABMultiValueRef addresses = ABRecordCopyValue(abRecordRef, kABPersonAddressProperty);
for (CFIndex index = 0; index < ABMultiValueGetCount(addresses); index++)
{
CFDictionaryRef properties = ABMultiValueCopyValueAtIndex(addresses, index);
NSString *street = [(__bridge NSString *)(CFDictionaryGetValue(properties, kABPersonAddressStreetKey)) copy];
NSLog(#"street: %#", street);
}
What am I doing wrong?
Even when debugging with the following:
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didSelectShippingAddress:(ABRecordRef)customShippingAddress
completion:(void (^)(PKPaymentAuthorizationStatus status, NSArray *methods, NSArray *items))completion
{
NSLog(#"%#", ABRecordCopyValue(customShippingAddress, kABPersonAddressProperty);
completion(PKPaymentAuthorizationStatusSuccess, ..., ...);
}
I get this with no street:
ABMultiValueRef 0x17227fbc0 with 1 value(s)
0: Shipping (0x17227fa00) - {
City = "Marina del Rey";
Country = "United States";
State = California;
ZIP = 90292;
} (0x172447440)
Edit:
I'm also experiencing issues with accessing names and phone attributes:
NSString *name = (__bridge_transfer NSString *)(ABRecordCopyCompositeName(abRecordRef));
NSString *fname = (__bridge_transfer NSString *)ABRecordCopyValue(abRecordRef, kABPersonFirstNameProperty);
NSString *lname = (__bridge_transfer NSString *)ABRecordCopyValue(abRecordRef, kABPersonFirstNameProperty);
if (!name && fname && lname) name = [NSString stringWithFormat:#"%# %#", fname, lname];
NSLog(#"name: %#", name); // nil
This is how the PKPaymentRequest is being created:
PKPaymentRequest *pr = [[PKPaymentRequest alloc] init];
[pr setMerchantIdentifier:#"********"];
[pr setCountryCode:#"US"];
[pr setCurrencyCode:#"USD"];
[pr setMerchantCapabilities:PKMerchantCapability3DS];
[pr setSupportedNetworks:#[PKPaymentNetworkAmex, PKPaymentNetworkVisa, PKPaymentNetworkMasterCard]];
[pr setPaymentSummaryItems:[self paymentSummaryItems]];
[pr setRequiredBillingAddressFields:PKAddressFieldAll];
[pr setRequiredShippingAddressFields:PKAddressFieldAll];
[pr setShippingMethods:[self supportedShippingMethods]];
Turns out Apple's docs on this weren't that great but the issue is that in the delegate callback for paymentAuthorizationViewController:didSelectShippingAddress:completion: a partial address is always returned. The fix is to also set it in the callback from:
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
// Use this instead.
[payment shippingAddress];
}
I also removed a call to setting the required billing addresses (maybe a separate bug).

Get values out of data from NSJSONSerialization

I have some JSON data which is pulled from a URL. The code I have written works fine to download the JSON and parse it, but I cannot seem to access it how I need too, especially where the data is contained as a sub-element of another one.
Here is the JSON format:
{
address = "<null>";
city = "<null>";
country = UK;
"country_code" = GB;
daylight = 1;
for = daily;
items = (
{
asr = "5:22 pm";
"date_for" = "2013-7-1";
dhuhr = "1:01 pm";
fajr = "2:15 am";
isha = "11:47 pm";
maghrib = "9:24 pm";
shurooq = "4:39 am";
}
);
latitude = "50.9994081";
link = "http://muslimsalat.com/UK";
longitude = "0.5039011";
"map_image" = "http://maps.google.com/maps/api/staticmap?center=50.9994081,0.5039011&sensor=false&zoom=13&size=300x300";
"postal_code" = "<null>";
"prayer_method_name" = "Muslim World League";
"qibla_direction" = "119.26";
query = "51.000000,0.500000";
state = "<null>";
timezone = 0;
title = UK;
"today_weather" = {
pressure = 1020;
temperature = 14;
};
}
(These are Islamic prayer times.)
My Objective-C so far is this:
-(CLLocationCoordinate2D) getLocation{
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
//class to convert JSON to NSData
- (IBAction)getDataFromJson:(id)sender {
//get the coords:
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"*dLatitude : %#", latitude);
NSLog(#"*dLongitude : %#",longitude);
//load in the times from the json
NSString *myURLString = [NSString stringWithFormat:#"http://muslimsalat.com/%#,%#/daily/5.json", latitude, longitude];
NSURL *url = [NSURL URLWithString:myURLString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
NSArray *jsonArray = (NSArray *)result; //convert to an array
if (error == nil)
NSLog(#"%#", result);
NSLog(#"%#", jsonArray);
for (id element in jsonArray) {
NSLog(#"Element: %#", [element description]);
}
}
}
When running this code, the only output I get is a list of element names (address, city, country, so on). items is given, but not its child elements. I understand that this is what I am asking the code for with:
for (id element in jsonArray) {
NSLog(#"Element: %#", [element description]);
}
but I do not know how to move onto the next step.
The only data values which I require are in fact the times themselves (so, items>asr, items>dhuhr, etc).
How can I get these values themselves and then save them as values I can work with?
Thank you!
(...); - is Array
{...}; - is Dictionary
so your "element" is Dictionary
use objectForKey:
example:
for (id element in jsonArray) {
NSLog(#"Element asr: %#", [element objectForKey:#"asr"]); // or element[#"asr"]
}
NSArray *jsonArray = (NSArray *)result; //convert to an array
This doesn't 'convert', it's just you promising the compiler that result is really an NSArray. And in this case it's a lie.
Your code is currently just printing a list of the keys in the dictionary that is returned in the JSON. Try this to get to the list of items (it's an array so you need to deal with there possibly being multiple entries):
NSDictionary *result = [NSJSONSerialization ...
for (NSDictionary *itemDict in result[#"items"]) {
NSLog(#"item: %#", itemDict);
}
Then you can extract the times.
You can extract info by following:
NSError* error = nil;
NSDictionary *userInfo; //your main data
if([NSJSONSerialization class])
userInfo = [NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];
//to extract items
NSDictionary *items = [[[userInfo objectForKey:#"items"] JSONValue] objectAtIndex:0];

Reverse Geocoding issues for City name in iOS

I'm able to retrieve the current location in my iPad application using,
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(#"-----------------Placemark is %#-----------------", placemarks);
locationLabel.text = placemarks;
}];
and the output is,
-----------------Placemark is ("South Atlantic Ocean, South Atlantic Ocean #<-42.60533670,-21.93128480> +/- 100.00m, region (identifier <-41.51023865,-31.60774370> radius 4954476.31) <-41.51023865,-31.60774370> radius 4954476.31m"
)-----------------
Can I use the same information to just get the city and the country name? instead of the long list of information?
also, the 'locationLabel.text = placemarks' gives a warning, "Incompatible pointer types assigning to 'NSString*' from 'NSArray*_strong', which I'm unable to resolve.
Yes you can.
But you doing it a little it wrong. First of all, placemarks is an array and not a string. That's why locationLabel.text = placemarks gives a warning.
Placemarks is an array of CLPlacemarks. This is because the geocoder could return multiple results for a coordinate. In the simplest condition the first item in it should be okay.
A CLPlacemark has the property addressDictionary which contains the data of this location.
You can access this data with the address property constans defined by the ABPerson header file.
For example:
Get the first placemark from the array:
CLPlacemark *place = [placemarks objectAtIndex:0];
then get the city from this placemark:
NSString *cityName = [place objectForKey: kABPersonAddressCityKey];
Don't forget to import the AVPerson header!
your can get all following place details
placeNameLabel.text = [placemarks[0] name];
addressNumberLabel.text = [placemarks[0] subThoroughfare];
addressLabel.text = [placemarks[0] thoroughfare];
neighborhoodLabel.text = [placemarks[0] subLocality];
cityLabel.text = [placemarks[0] locality];
countyLabel.text = [placemarks[0] subAdministrativeArea];
stateLabel.text = [placemarks[0] administrativeArea];
zipCodeLabel.text = [placemarks[0] postalCode];
countryLabel.text = [placemarks[0] country];
countryCodeLabel.text = [placemarks[0] ISOcountryCode];
inlandWaterLabel.text = [placemarks[0] inlandWater];
oceanLabel.text = [placemarks[0] ocean];

Resources