How can I plot addresses in Swift, converting address to longitude and latitude coordinates? - objective-c

In Objective-C this code works fine to plot an address and it finds the longitude and latitude coordinates:
NSString *address = #"1 Infinite Loop, CA, USA";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address
completionHandler:^(NSArray* placemarks, NSError* error){
// Check for returned placemarks
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create a MLPlacemark and add it to the map view
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
[self.mapView addAnnotation:placemark];
[placemark release];
}
[geocoder release];
}];
I have been searching for days on how to do this with Swift?! Could anyone point me in the right direction? Could you explain if a user needs an internet connection for this to work. Sorry if these questions sounds a bit basic, but I am quite new to this and learning every day.
I understand that I have to use this code, but cannot find a way to implement it
func geocodeAddressString(_ addressString: String!,
completionHandler completionHandler: CLGeocodeCompletionHandler!)

XCode 9 & Swift 3:
import CoreLocation
let address = "1 Infinite Loop, CA, USA"
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
}
})

Try something like
var address = "1 Infinite Loop, CA, USA"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
}
})

var address = "1 Infinite Loop, CA, USA"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) {
if let placemarks = $0 {
println(placemarks)
} else {
println($1)
}
}

Related

How to get lat and long coordinates from address string

I have a MKMapView that has a UISearchBar on the top, and I want the user to be able to type a address, and to find that address and drop a pin on it. What I don't know is how to turn the address string into longitude and latitude, so I can make a CLLocation object. Does anyone know how I can do this?
You may find your answer in this question.
iOS - MKMapView place annotation by using address instead of lat / long By User Romes.
NSString *location = #"some address, state, and zip";
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 = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
}
}
];
A Very simple solution. But only applicable on iOS5.1 or later.
I used a similar approach like Vijay, but had to adjust one line of code. region.center = placemark.region.center didn't work for me. Maybe my code helps someone as well:
let location: String = "1 Infinite Loop, CA, USA"
let geocoder: CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if (placemarks?.count > 0) {
let topResult: CLPlacemark = (placemarks?[0])!
let placemark: MKPlacemark = MKPlacemark(placemark: topResult)
var region: MKCoordinateRegion = self.mapView.region
region.center.latitude = (placemark.location?.coordinate.latitude)!
region.center.longitude = (placemark.location?.coordinate.longitude)!
region.span = MKCoordinateSpanMake(0.5, 0.5)
self.mapView.setRegion(region, animated: true)
self.mapView.addAnnotation(placemark)
}
})
For swift2
var location: String = "some address, state, and zip"
var geocoder: CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if (placemarks?.count > 0) {
var topResult: CLPlacemark = (placemarks?[0])!
var placemark: MKPlacemark = MKPlacemark(placemark: topResult)
var region: MKCoordinateRegion = self.mapView.region
region.center = placemark.region.center
region.span.longitudeDelta /= 8.0
region.span.latitudeDelta /= 8.0
self.mapView.setRegion(region, animated: true)
self.mapView.addAnnotation(placemark)
}
})
func geoCodeUsingAddress(address: NSString) -> CLLocationCoordinate2D {
var latitude: Double = 0
var longitude: Double = 0
let addressstr : NSString = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(address)" as NSString
let urlStr = addressstr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let searchURL: NSURL = NSURL(string: urlStr! as String)!
do {
let newdata = try Data(contentsOf: searchURL as URL)
if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary {
print(responseDictionary)
let array = responseDictionary.object(forKey: "results") as! NSArray
let dic = array[0] as! NSDictionary
let locationDic = (dic.object(forKey: "geometry") as! NSDictionary).object(forKey: "location") as! NSDictionary
latitude = locationDic.object(forKey: "lat") as! Double
longitude = locationDic.object(forKey: "lng") as! Double
} catch {
}
}
var center = CLLocationCoordinate2D()
center.latitude = latitude
center.longitude = longitude
return center
}

How to retrieve the address from a location returned by Core Location

Is there a way to turn a longitude/latitude location (as returned by Core Location) into an address (street and city)?
The best way is using reverse geocoding, which is available in CLGeocoder class. To get human readable address from geoposition, you have to use reverseGeocodeLocation method.
Here is small sample:
-(NSString *)getAddressFromLocation:(CLLocation *)location {
NSString *address;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(placemarks && placemarks.count > 0)
{
CLPlacemark *placemark= [placemarks objectAtIndex:0];
address = [NSString stringWithFormat:#"%# %#,%# %#", [placemark subThoroughfare],[placemark thoroughfare],[placemark locality], [placemark administrativeArea]];
NSLog(#"%#",address);
}
}];
[geocoder release];
return address;
}
Rough Swift 3 implementation using closures:
import CoreLocation
func getAddressFrom(location: CLLocation, completion:#escaping ((String?) -> Void)) {
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
if let placemark = placemarks?.first,
let subThoroughfare = placemark.subThoroughfare,
let thoroughfare = placemark.thoroughfare,
let locality = placemark.locality,
let administrativeArea = placemark.administrativeArea {
let address = subThoroughfare + " " + thoroughfare + ", " + locality + " " + administrativeArea
placemark.addressDictionary
return completion(address)
}
completion(nil)
}
}
Usage:
getAddressFrom(location: location) { (address) in
print(address)
}
You can also look at placemark.addressDictionary, which is dictionary containing the Address Book keys and values for the placemark. These keys are defined in the Address Book framework.

Issue forward-geocoding multiple addresses

I am connecting to a remote web service which basically returns an XML back. I am then parsing that XML into a Property object (think real state sort of thing)
But now, the web service returns a postal code for each property alone. It does not provide a coordinate which is what I need to place an annotation in the map. I am able to geocode an address provided a postal code. However, my problem is it is not allowing me to do multiple requests
Here's my code
- (void)processProperties:(Property *)property {
[geocoder geocodeAddressString:property.postalCode
completionHandler:^(NSArray* placemarks, NSError* error){
placemark = [placemarks lastObject];
for (CLPlacemark* aPlacemark in placemarks)
{
[sublet setLatitude:aPlacemark.location.coordinate.latitude];
[sublet setLongitude:aPlacemark.location.coordinate.longitude];
}
}];
}
- (void)addAnnotations:(NSArray *)objects {
CLLocationDegrees lat;
CLLocationDegrees longitude;
CLLocationCoordinate2D mCoords;
NSString *fullAddress;
// Add the annotations found nearby
for (Property *property in objects) {
[self processProperties:property];
lat = property.latitude;
longitude = property.longitude;
fullAddress = [NSString stringWithFormat:#"%# %# %#", property.houseNumber, #" ", property.streetName];
[self createAnnotationWithCoords:mCoords :fullAddress :[NSString stringWithFormat:#"$%.2f", property.rent]];
}
zoomLevel = 0.1;
mCoords = CLLocationCoordinate2DMake(lat,longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(mCoords,MKCoordinateSpanMake(zoomLevel,zoomLevel));
[self.mapView setRegion:region animated:YES];
}
For some reason it's just geocoding 1 property. Is not going through the loop accordingly.
Any ideas folks?
Use this on your Forward Geo Function. geocoder needs to be release and initialized again to start a new address, hope this helps.
- (void)processProperties:(Property *)property {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:property.postalCode
completionHandler:^(NSArray* placemarks, NSError* error){
placemark = [placemarks lastObject];
for (CLPlacemark* aPlacemark in placemarks)
{
[sublet setLatitude:aPlacemark.location.coordinate.latitude];
[sublet setLongitude:aPlacemark.location.coordinate.longitude];
}
[geocoder release];
}];
}

iPhone Developement : Why does GKLeaderboard loadScoresWithCompletionHandler: return a null value

I'm working With Game Center right now and I have a issue with GC.
When I'm using initWithPlayerIDs:, I don't get any score when loadScoresWithCompletionHandler: callback is called.
GKLeaderboard *leaderBoard = [[[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:gcPlayerID]] autorelease];
leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
leaderBoard.category = #"SomeLeaderboard";
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error == nil)
{
// scores is null
// ...
}
But when I'm doing:
GKLeaderboard *leaderBoard = [[[GKLeaderboard alloc] init] autorelease];
leaderBoard.timeScope = GKLeaderboardTimeScopeAllTime;
leaderBoard.category = #"SomeLeaderboard";
[leaderBoard loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error == nil)
{
for (GKScore* score in scores)
if ([score.playerID isEqualToString:gcPlayerID])
{
// Got something here
return;
}
}
It's working.
I'm using the 2nd method at the moment but it will make time to process if there is many score.
Does anyone have the same issue ?
Thanks.
I'm afraid that with the given info I don't have an answer as to why initWithPlayerIDs: is not working. However, I might be able to simplify your filtering for the local player score in the 2nd method. A GKLeaderboard has a property localPlayerScore that is valid only after loadScoresWithCompletionHandler: has completed. localPlayerScore then gives the GKScore for the local player. Your 2nd method would then look like this:
[leaderBoard loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if (error == nil)
{
GKScore* myScore = leaderboard.localPlayerScore;
}
}
Hope this helps a little.
Try to narrow the type of scores to those you really want to process. For example request the score for the logged in player and for global time scope etc.
I am using something like the following code snippet in my own game and it is fast:
// Load score for player
GKLeaderboard *board = [[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:myGCPlayerID]];
board.timeScope = GKLeaderboardTimeScopeAllTime;
board.playerScope = GKLeaderboardPlayerScopeGlobal;
board.category = #"myGCCategory";
[board loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if(error != nil) {
NSLog(#"Error loading score:\n%#", [error localizedDescription]);
}
if(scores != nil) {
for(int i=0; i<[scores count]; i++) {
GKScore *score = (GKScore *)[scores objectAtIndex:i];
if(([score.playerID isEqualToString:myGCPlayerID]) &&
(score.value > playerLastScore)) {
playerLastScore = score.value;
}
}
}
}];
[board release];
You may also apply a more optimized score loop as it is in your code. I hope this helps.

IP Address? - Cocoa

How would I make a GUI program that displays your Ip address with a click of a button? Please, no difficult explanations, I just started Cocoa not long ago.
Thanks,
Kevin
You can get IP address through two ways:
1- if you want to get the local ip address on the current used netwrok, you can use the following method to retrive it:
-(NSString *)getIPAddress
{
NSString *address = #"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
2- if you want to get the external IP address then you need to use the following method:
-(NSString*)getIP
{
NSUInteger an_Integer;
NSArray * ipItemsArray;
NSString *externalIP;
NSURL *iPURL = [NSURL URLWithString:#"http://www.dyndns.org/cgi-bin/check_ip.cgi"];
if (iPURL) {
NSError *error = nil;
NSString *theIpHtml = [NSString stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding error:&error];
if (!error) {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:theIpHtml];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:#"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:#">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:#"%#>", text]
withString:#" "] ;
ipItemsArray =[theIpHtml componentsSeparatedByString:#" "];
an_Integer=[ipItemsArray indexOfObject:#"Address:"];
externalIP =[ipItemsArray objectAtIndex: ++an_Integer];
}
NSLog(#"%#",externalIP);
} else {
NSLog(#"Oops... g %d, %#", [error code], [error localizedDescription]);
}
}
return externalIP;
}
For determining the IP address, I found
this.
As for making it into a Cocoa app, add an NSTextField (label) to your main window in Interface Builder, put in a button, add in an application controller (a subclass of NSObject that you make), put in the outlet and the action, do the proper connenctions, and in the "get IP" method, put in that code and set the value for the label's stringValue.
You can use [[NSHost currentHost] address], but it won't always display what you like. On my system, for example, it gives my IPv6 address.
EDIT: On my system, [[[NSHost currentHost] addresses] objectAtIndex:0] has my IPv4 address.
[[NSHost currentHost] addresses] will get you an array of IPs. Read the documentation for NSHost.
As for displaying that in a GUI, I recommend getting Aaron Hillegass' book Cocoa Programming for Mac OS X, or any Cocoa beginners book should teach that.
I just wrote this, may need some work but seems to work well on my machine...
- (NSString *)getLocalIPAddress
{
NSArray *ipAddresses = [[NSHost currentHost] addresses];
NSArray *sortedIPAddresses = [ipAddresses sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.allowsFloats = NO;
for (NSString *potentialIPAddress in sortedIPAddresses)
{
if ([potentialIPAddress isEqualToString:#"127.0.0.1"]) {
continue;
}
NSArray *ipParts = [potentialIPAddress componentsSeparatedByString:#"."];
BOOL isMatch = YES;
for (NSString *ipPart in ipParts) {
if (![numberFormatter numberFromString:ipPart]) {
isMatch = NO;
break;
}
}
if (isMatch) {
return potentialIPAddress;
}
}
// No IP found
return #"?.?.?.?";
}
We can use hostWithName: method with current host name. This will return only single local IPv4 and IPv6 IP, which we can filter easily.
We can get the current system host name using [[NSHost currentHost] name].
+(NSString *)getLocalIPAddress{
NSArray *ipAddresses = [[NSHost hostWithName:[[NSHost currentHost] name]] addresses];
for (NSString *ipAddress in ipAddresses) {
if ([ipAddress componentsSeparatedByString:#"."].count == 4) {
return ipAddress;
}
}
return #"Not Connected.";
}
So, this will solve all the problems mentions in comments of other answers. Also, this significantly work more faster than other solution mention here.