Get current Geolocation and Show it on the maps

I thought here, why not combine 2 groups of handlers from users @Shane Stanley and @maro to solve my problem. The result was the following script, which I hope will be useful to other users.


-- script: Get current Geolocation and Show it on maps

-- written by @KniazidisR (today)
-- great handlers: written by users @Shane Stanley and @maro (thanks)

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "MapKit"
use framework "CoreLocation" -- for location

property NSAlert : a reference to current application's NSAlert
property NSString : a reference to current application's NSString
property NSScreen : a reference to current application's NSScreen
property MKMapView : a reference to current application's MKMapView
property MKMapTypeHybrid : a reference to current application's MKMapTypeHybrid
property MKPointAnnotation : a reference to current application's MKPointAnnotation
property MKMapTypeSatellite : a reference to current application's MKMapTypeSatellite
property MKMapTypeStandard : a reference to current application's MKMapTypeStandard
property NSSegmentedControl : a reference to current application's NSSegmentedControl
property NSRunningApplication : a reference to current application's NSRunningApplication
property NSSegmentStyleTexturedRounded : a reference to current application's NSSegmentStyleTexturedRounded
property selSeg : 0
property aMapViewList : {}
property aLatitude : 0.0
property aLongitude : 0.0


if not current application's CLLocationManager's locationServicesEnabled() as boolean then
	error "Location Services not enabled"
end if
if current application's NSThread's isMainThread() as boolean then
	my startSearching()
else
	my performSelectorOnMainThread:"startSearching" withObject:(missing value) waitUntilDone:true
end if
delay 1 -- required
display dialog "Latitude:     " & aLatitude & linefeed & "Longitude:  " & aLongitude

set paramObj to {viewWidth:1000, viewHeight:600, viewSubTitle:"Geolocation Showing Example", viewLat:aLatitude, viewLong:aLongitude}
my performSelectorOnMainThread:"dispMapViewinDifferentScales:" withObject:(paramObj) waitUntilDone:true


----------------------------  Handlers from the user @Shane Stanley (thanks)  -------------------------------------

on startSearching()
	set locationManager to current application's CLLocationManager's alloc()'s init()
	locationManager's setDelegate:me
	locationManager's setDesiredAccuracy:(current application's kCLLocationAccuracyThreeKilometers)
	locationManager's startUpdatingLocation()
end startSearching

on locationManager:locationManager didUpdateLocations:newLocations
	locationManager's stopUpdatingLocation()
	set newLocation to newLocations's lastObject()
	set theCoordinates to newLocation's coordinate()
	set aLatitude to theCoordinates's latitude()
	set aLongitude to theCoordinates's longitude()
	set coords to ((latitude of theCoordinates) as text) & linefeed & longitude of theCoordinates
end locationManager:didUpdateLocations:

on locationManager:anCLLocationManager didFailWithError:anNSError
	error (anNSError's localizedDescription() as text)
end locationManager:didFailWithError:


---------------------------------------  Handlers from the user @maro (thanks) -------------------------------------

on dispMapViewinDifferentScales:paramObj
	set aWidth to (viewWidth of paramObj) as real
	set aHeight to (viewHeight of paramObj) as real
	set aLat to (viewLat of paramObj) as real
	set aLong to (viewLong of paramObj) as real
	--set aTitle to (viewTitle of paramObj) as string
	set aSubTitle to (viewSubTitle of paramObj) as string
	
	set selSeg to 0
	set aView to current application's NSView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
	set wList to {{3, "????World Level Map"}, {5, "????Country Level Map"}, {10, "????City Level Map"}, {17, "????Town Level Map"}}
	set xPos to 0
	repeat with i in wList
		copy i to {aLevelNum, aBoxTitle}
		set aBox to (current application's NSBox's alloc()'s initWithFrame:(current application's NSMakeRect(xPos, 40, aWidth * 0.25, aHeight - 70)))
		(aBox's setTitle:aBoxTitle)
		set aMapView to makeMKMapView(aWidth * 0.25, aHeight - 70, aLevelNum, aLat, aLong) of me
		(aBox's addSubview:aMapView)
		(aView's addSubview:aBox)
		set the end of aMapViewList to aMapView
		set xPos to xPos + (aWidth * 0.25)
	end repeat
	-- Segmented Control
	set aSeg to makeSegmentedControl({"Map", "Satellite", "Satellite + Map"}, aWidth, aHeight) of me
	aView's addSubview:aSeg
	-- Set up alert	
	set theAlert to NSAlert's alloc()'s init()
	tell theAlert
		its setMessageText:""
		its setInformativeText:aSubTitle
		its addButtonWithTitle:"OK"
		its addButtonWithTitle:"Cancel"
		its setAccessoryView:aView
	end tell
	-- Show alert in modal loop
	NSRunningApplication's currentApplication()'s activateWithOptions:0
	my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
	if (my returnCode as number) = 1001 then error number -128
end dispMapViewinDifferentScales:

on doModal:aParam
	set (my returnCode) to aParam's runModal()
end doModal:

on makeMKMapView(aWidth, aHeight, aZoomLevel, aLat, aLong)
	set aMapView to MKMapView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
	aMapView's setMapType:(current application's MKMapTypeStandard)
	
	aMapView's setZoomEnabled:true
	aMapView's setScrollEnabled:true
	aMapView's setPitchEnabled:false
	aMapView's setRotateEnabled:false
	aMapView's setShowsCompass:true
	aMapView's setShowsZoomControls:true
	aMapView's setShowsScale:true
	aMapView's setShowsUserLocation:true
	set aLocation to current application's CLLocationCoordinate2DMake(aLat, aLong)
	aMapView's setCenterCoordinate:aLocation zoomLevel:aZoomLevel animated:false
	aMapView's setDelegate:me
	-- Map Pin
	set anAnnotation to current application's MKPointAnnotation's alloc()'s init()
	anAnnotation's setCoordinate:aLocation
	anAnnotation's setTitle:""
	aMapView's addAnnotation:anAnnotation
	return aMapView
end makeMKMapView

on makeSegmentedControl(titleList, aWidth, aHeight)
	set aLen to length of titleList
	set aSeg to NSSegmentedControl's alloc()'s init()
	aSeg's setSegmentCount:aLen
	set aCount to 0
	repeat with i in titleList
		set j to contents of i
		(aSeg's setLabel:j forSegment:aCount)
		set aCount to aCount + 1
	end repeat
	aSeg's setTranslatesAutoresizingMaskIntoConstraints:false
	aSeg's setSegmentStyle:(NSSegmentStyleTexturedRounded)
	aSeg's setFrame:(current application's NSMakeRect(10, 5, 260, 30))
	aSeg's setTrackingMode:0
	aSeg's setTarget:me
	aSeg's setAction:"clickedSeg:"
	aSeg's setSelectedSegment:0
	return aSeg
end makeSegmentedControl

on clickedSeg:aSender
	set aSel to aSender's selectedSegment()
	set selSeg to (aSel + 1)
	set mapList to {MKMapTypeStandard, MKMapTypeSatellite, MKMapTypeHybrid}
	set curMap to contents of item selSeg of mapList
	repeat with i in aMapViewList
		set aView to contents of i
		(aView's setMapType:(curMap))
	end repeat
end clickedSeg: