1""" 2This file is a translation from Objective-C. The original 3copy-right: 4 5// Copyright 2009 Matt Gallagher. All rights reserved. 6// 7// Permission is given to use this source code file, free of charge, in any 8// project, commercial or otherwise, entirely at your risk, with the condition 9// that any redistribution (in part or whole) of source code must retain 10// this copyright and permission notice. Attribution in compiled projects is 11// appreciated but not required. 12""" 13 14from Cocoa import * 15import WebKit 16from CoreLocation import * 17import math 18 19class WhereIsMyMacAppDelegate (NSObject): 20 window = objc.ivar() 21 webView = objc.IBOutlet() 22 locationManager = objc.ivar() 23 locationLabel = objc.IBOutlet() 24 accuracyLabel = objc.IBOutlet() 25 26 27 28 def applicationDidFinishLaunching_(self, notification): 29 self.locationManager = CLLocationManager.alloc().init() 30 self.locationManager.setDelegate_(self) 31 self.locationManager.startUpdatingLocation() 32 33 @classmethod 34 def latitudeRangeForLocation_(self, aLocation): 35 M = 6367000.0; # approximate average meridional radius of curvature of earth 36 metersToLatitude = 1.0 / ((math.pi / 180.0) * M); 37 accuracyToWindowScale = 2.0; 38 39 return aLocation.horizontalAccuracy() * metersToLatitude * accuracyToWindowScale; 40 41 @classmethod 42 def longitudeRangeForLocation_(self, aLocation): 43 latitudeRange = WhereIsMyMacAppDelegate.latitudeRangeForLocation_(aLocation) 44 45 return latitudeRange * math.cos(aLocation.coordinate().latitude * math.pi / 180.0) 46 47 48 @objc.IBAction 49 def openInDefaultBrowser_(self, sender): 50 currentLocation = locationManager.location() 51 52 externalBrowserURL = NSURL.URLWithString_( 53 u"http://maps.google.com/maps?ll=%f,%f&spn=%f,%f"%( 54 currentLocation.coordinate.latitude, 55 currentLocation.coordinate.longitude, 56 WhereIsMyMacAppDelegate.latitudeRangeForLocation_(currentLocation), 57 WhereIsMyMacAppDelegate.longitudeRangeForLocation_(currentLocation))) 58 59 NSWorkspace.sharedWorkspace.openURL_(externalBrowserURL) 60 61 def locationManager_didUpdateToLocation_fromLocation_(self, 62 manager, newLocation, oldLocation): 63 64 # Ignore updates where nothing we care about changed 65 if newLocation is None: 66 return 67 if oldLocation is None: 68 pass 69 elif (newLocation.coordinate().longitude == oldLocation.coordinate().longitude and 70 newLocation.coordinate().latitude == oldLocation.coordinate().latitude and 71 newLocation.horizontalAccuracy() == oldLocation.horizontalAccuracy()): 72 return 73 74 # Load the HTML for displaying the Google map from a file and replace the 75 # format placeholders with our location data 76 path = NSBundle.mainBundle().pathForResource_ofType_(u"HTMLFormatString", u"html") 77 htmlString = open(path, 'r').read() % ( 78 newLocation.coordinate().latitude, 79 newLocation.coordinate().longitude, 80 WhereIsMyMacAppDelegate.latitudeRangeForLocation_(newLocation), 81 WhereIsMyMacAppDelegate.longitudeRangeForLocation_(newLocation)) 82 83 # Load the HTML in the WebView and set the labels 84 self.webView.mainFrame().loadHTMLString_baseURL_(htmlString, None) 85 self.locationLabel.setStringValue_(u"%f, %f"%( 86 newLocation.coordinate().latitude, newLocation.coordinate().longitude)) 87 self.accuracyLabel.setStringValue_(u"%f"%(newLocation.horizontalAccuracy(),)) 88 89 def locationManager_didFailWithError_(self, manager, error): 90 self.webView.mainFrame.loadHTMLString_baseURL_( 91 NSLocalizedString(u"Location manager failed with error: %s", nil) % ( 92 error.localizedDescription()), None) 93 self.locationLabel.setStringValue_(u"") 94 self.accuracyLabel.setStringValue_(u"") 95 96 def applicationWillTerminate_(self, aNotification): 97 self.locationManager.stopUpdatingLocation() 98