WhereIsMyMacAppDelegate.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-framework-CoreLocation » Examples » WhereIsMyMac » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc framework CoreLocation » Examples » WhereIsMyMac » WhereIsMyMacAppDelegate.py
"""
This file is a translation from Objective-C. The original
copy-right:

//  Copyright 2009 Matt Gallagher. All rights reserved.
//
//  Permission is given to use this source code file, free of charge, in any
//  project, commercial or otherwise, entirely at your risk, with the condition
//  that any redistribution (in part or whole) of source code must retain
//  this copyright and permission notice. Attribution in compiled projects is
//  appreciated but not required.
"""

from Cocoa import *
import WebKit
from CoreLocation import *
import math

class WhereIsMyMacAppDelegate (NSObject):
    window = objc.ivar()
    webView = objc.IBOutlet()
    locationManager = objc.ivar()
    locationLabel = objc.IBOutlet()
    accuracyLabel = objc.IBOutlet()



    def applicationDidFinishLaunching_(self, notification):
  self.locationManager = CLLocationManager.alloc().init()
  self.locationManager.setDelegate_(self)
  self.locationManager.startUpdatingLocation()

    @classmethod
    def latitudeRangeForLocation_(self, aLocation):
  M = 6367000.0; # approximate average meridional radius of curvature of earth
  metersToLatitude = 1.0 / ((math.pi / 180.0) * M);
  accuracyToWindowScale = 2.0;
  
  return aLocation.horizontalAccuracy() * metersToLatitude * accuracyToWindowScale;

    @classmethod
    def longitudeRangeForLocation_(self, aLocation):
  latitudeRange = WhereIsMyMacAppDelegate.latitudeRangeForLocation_(aLocation)
  
  return latitudeRange * math.cos(aLocation.coordinate().latitude * math.pi / 180.0)


    @objc.IBAction
    def openInDefaultBrowser_(self, sender):
  currentLocation = locationManager.location()
  
  externalBrowserURL = NSURL.URLWithString_(
    u"http://maps.google.com/maps?ll=%f,%f&spn=%f,%f"%(
                    currentLocation.coordinate.latitude,
                    currentLocation.coordinate.longitude,
                    WhereIsMyMacAppDelegate.latitudeRangeForLocation_(currentLocation),
                    WhereIsMyMacAppDelegate.longitudeRangeForLocation_(currentLocation)))

  NSWorkspace.sharedWorkspace.openURL_(externalBrowserURL)

    def locationManager_didUpdateToLocation_fromLocation_(self, 
            manager, newLocation, oldLocation):

  # Ignore updates where nothing we care about changed
        if newLocation is None:
            return
        if oldLocation is None:
            pass
  elif (newLocation.coordinate().longitude == oldLocation.coordinate().longitude and
    newLocation.coordinate().latitude == oldLocation.coordinate().latitude and
                newLocation.horizontalAccuracy() == oldLocation.horizontalAccuracy()):
    return

  # Load the HTML for displaying the Google map from a file and replace the
  # format placeholders with our location data
        path = NSBundle.mainBundle().pathForResource_ofType_(u"HTMLFormatString", u"html")
        htmlString = open(path, 'r').read() % (
    newLocation.coordinate().latitude,
    newLocation.coordinate().longitude,
    WhereIsMyMacAppDelegate.latitudeRangeForLocation_(newLocation),
    WhereIsMyMacAppDelegate.longitudeRangeForLocation_(newLocation))
  
  # Load the HTML in the WebView and set the labels
  self.webView.mainFrame().loadHTMLString_baseURL_(htmlString, None)
  self.locationLabel.setStringValue_(u"%f, %f"%(
    newLocation.coordinate().latitude, newLocation.coordinate().longitude))
  self.accuracyLabel.setStringValue_(u"%f"%(newLocation.horizontalAccuracy(),))

    def locationManager_didFailWithError_(self, manager, error):
  self.webView.mainFrame.loadHTMLString_baseURL_(
                NSLocalizedString(u"Location manager failed with error: %s", nil) % (
                    error.localizedDescription()), None)
  self.locationLabel.setStringValue_(u"")
  self.accuracyLabel.setStringValue_(u"")

    def applicationWillTerminate_(self, aNotification):
  self.locationManager.stopUpdatingLocation()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.