PyObjC MapKit Framework

12.1 · active · verified Tue Apr 14

pyobjc-framework-mapkit provides Python bindings for Apple's MapKit framework on macOS, enabling Python applications to integrate maps, display points of interest, and manage map data. It is part of the larger PyObjC project, currently at version 12.1, and follows a release cadence tied to macOS SDK updates and Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic macOS application window using AppKit, then embed and display an MKMapView from the MapKit framework, centering it on a specific geographical coordinate. It requires running on macOS with a functional PyObjC installation.

import objc
from AppKit import NSApplication, NSWindow, NSView, NSMakeRect, NSApp
from Foundation import NSObject
from MapKit import MKMapView, CLLocationCoordinate2D, MKCoordinateSpan, MKCoordinateRegion

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, notification):
        print("PyObjC MapKit Application Launched!")
        # Create a window
        self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
            NSMakeRect(100, 100, 800, 600),
            (NSWindow.NSTitledWindowMask | NSWindow.NSClosableWindowMask | NSWindow.NSResizableWindowMask),
            NSWindow.NSBackingStoreBuffered,
            False
        )
        self.window.setTitle_("PyObjC MapKit Example")
        
        # Create an MKMapView
        self.mapView = MKMapView.alloc().initWithFrame_(self.window.contentView().frame())
        self.mapView.setAutoresizingMask_(NSView.NSViewWidthSizable | NSView.NSViewHeightSizable)
        
        # Define a location (e.g., San Francisco)
        coordinate = CLLocationCoordinate2D(latitude=37.7749, longitude=-122.4194)
        span = MKCoordinateSpan(latitudeDelta=0.1, longitudeDelta=0.1)
        region = MKCoordinateRegion(center=coordinate, span=span)
        
        self.mapView.setRegion_(region)
        
        # Add the map view to the window
        self.window.contentView().addSubview_(self.mapView)
        
        self.window.makeKeyAndOrderFront_(None)

    def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
        return True

# Initialize and run the application
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
NSApp.run()

view raw JSON →