CoreLocation

Anyone know how I can use CoreLocation in my program. I found this: http://cocoawithlove.com/2009/09/whereismymac-snow-leopard-corelocation.html

But i wouldn’t know how to adapt that to ASOC. I also need the zip code if thats possible to get instead of latitude and longitude coordinates.

You do not need to convert the code to AppleScriptObjC. Use the class files and modify them to fit your specific needs.

To find a zip code using lat and long, I found this site with a db for just this purpose.
http://www.boutell.com/zipcodes/

Would it be possible to convert the parts I need to ASOC? I just have a really hard time understanding Obj-C

anyone?

I experimented with the code in the file you linked to and came up with the following as a start:

script LocatorAppDelegate
	property parent : class "NSObject"
	property CLLocationManager : class "CLLocationManager"
	
	on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
		log newLocation
	end locationManager_didUpdateToLocation_fromLocation_
	
	on applicationWillFinishLaunching_(aNotification)
		set locationManager to CLLocationManager's alloc()'s init()
		locationManager's setDelegate_(me)
		locationManager's startUpdatingLocation()
		log locationManager's |location|()
		locationManager's stopUpdatingLocation()
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_

You have to add the CoreLocation framework to your project to get this working (Click on the Frameworks triangle in the groups and Files pane of Xcode, then right click on the Other Frameworks folder, choose Add and go down to Existing Frameworks. Choose CoreLocation.framework and click Add).

I can’t really tell whether this is working properly since I’m not near any WiFi networks that would have any location info associated with them. The startUpdatingLocation() method seems to be working, because it opens up a panel asking if you want your program to use your current location. The log of locationManager’s |location| gives me “null” which I would expect given that my computer is not connected to any public WiFi network. I’m not sure what I should be passing in to “locationManager_didUpdateToLocation_fromLocation_”. It isn’t being called in this program, but I’m not sure that it would be if I’m not getting any location info. I’d be interested to know if the log gives you any info if you try this.

I’m getting null also. CoreLocation on my iPod touch gets my location so i shouldn’t be getting null.

I don’t know about that - the iPhone and the computer may be accessing different data to get location. It isn’t clear to me how the device is getting this information. I downloaded the app from the link you posted and added “NSLog(@”%@“,[locationManager location])” to the end of his applicationDidFinishLaunching method, and it also logged “null”(and I didn’t get any results in the program window).

Hmm, i just tried that. It logs null, but it also does get my location. Maybe we’re logging the wrong variable?

I do think that location is the right thing to log as far as I can tell from the Objective-C. It, and pieces derived from it like location.coordinates.latitude seem to be where the data is coming from. I don’t know if there can be a timing issue – whether it takes some time to get the data. I tried putting a delay in my apple script, but that didn’t help. I’m not sure how to put a delay in Objective-C.

in Obj-C I think is sleep(); to put a delay. I’ll play around with it and see if i can find anything.

By the way, I added the following method to my program that I posted above:

on locationManager_didFailWithError_(locationManager, myError)
		log myError
	end locationManager_didFailWithError_

This logs an error about 42 seconds after I start the program with this message:

2010-04-27 21:08:39.655 Locator[29356:a0f] Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLErrorDomain error 0.)”

I’m not getting an error, so I guess it is getting my location.

Ok, i got it.

I changed

locationManager's |location|()

to

locationManager's newLocation

and it logs my coordinates, altitude, speed and course!

Thanks for the help!

Where in the program did you change locationManager’s |location|() to locationManager’s newLocation? I don’t understand why that would work since newLocation isn’t a Cocoa property, it’s just used as a parameter name in the “locationManager:didUpdateToLocation:fromLocation:” method.

Just the log statement in applicationWillFinishLaunching_

EDIT: it was working, but now it doesn’t anymore

Ok, I got it. Here’s the code

script LocatorAppDelegate
   property parent : class "NSObject"
   property CLLocationManager : class "CLLocationManager"
   
   on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)
       log newLocation
   end locationManager_didUpdateToLocation_fromLocation_
   
   on applicationWillFinishLaunching_(aNotification)
       set locationManager to CLLocationManager's alloc()'s init()
       locationManager's setDelegate_(me)
       locationManager's startUpdatingLocation()
       locationManager's newLocation
       locationManager's stopUpdatingLocation()
   end applicationWillFinishLaunching_
   
   on applicationShouldTerminate_(sender)
       -- Insert code here to do any housekeeping before your application quits 
       return current application's NSTerminateNow
   end applicationShouldTerminate_

It logs the location in the locationManager_didUpdateToLocation_fromLocation_ method. However, I can’t seem to manipulate the data it gets. How do I get it as a string?

I don’t know if you can get the whole thing as a string, but you should be able to get the various parts of the data with things like:

on locationManager_didUpdateToLocation_fromLocation_(locationManager, newLocation, oldLocation)	
	log newLocation's coordinate()'s latitude()
	log newLocation's coordinate()'s longitude()		
	log newLocation's horizontalAccuracy()
end locationManager_didUpdateToLocation_fromLocation_

I’m not sure what the line, “locationManager’s newLocation” in the applicationWillFinishLaunching_ method is doing. If you comment out that line out does your program still work?

that doesn’t work. I get this error:

*** -[MenuWeatherAppDelegate locationManager:didUpdateToLocation:fromLocation:]: -[BAImmediateData latitude]: unrecognized selector sent to instance 0x200489920 (error -10000)

I don’t know where to go from here. I think that what I gave you is a good translation from the Objective-C code, but I’m not really sure what kind of objects “coordinate” and “latitude” are. You could try getting rid of the parentheses in coordinate() and/or latitude() and see if that helps. When you log newLocation, what do you get?

I tried removing the (), and all other sorts of combinations. Here’s what I get when I log newLocation:

<+43.00548482, -78.78936669> +/- 157.00m (speed 0.00 mps / course -1.00) @ 2010-04-29 13:44:45 -0400

Everything I need is there, but I can’t manipulate it in anyway or get it as a string.