Platforms to show: All Mac Windows Linux Cross-Platform
/MacFrameworks/MapKit/MapKit Local Search
Required plugins for this example: MBS MacBase Plugin, MBS MacFrameworks Plugin, MBS MacCG Plugin, MBS MacCF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/MapKit/MapKit Local Search
This example is the version from Tue, 13th Apr 2020.
Project "MapKit Local Search.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
if not MKMapViewMBS.available then
MsgBox "Please run on Mac 64-bit."
end if
End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MainWindow Inherits Window
Control SearchText Inherits TextField
ControlInstance SearchText Inherits TextField
EventHandler Function KeyDown(Key As String) As Boolean
If StartSearchWithReturnKeyOnly Then
If Asc(key) = 3 Or Asc(key) = 13 Then // return keys
Completer.QueryFragment = Me.Text
Return True
End If
End If
End EventHandler
EventHandler Sub TextChange()
If StartSearchWithReturnKeyOnly Then
// only clear the ListBoxes the first time a key is pressed
// as they won't get filled again until a search is initiated
If SuggestionsList.ListCount > 0 Then SuggestionsList.DeleteAllRows
If ResultList.ListCount > 0 Then ResultList.DeleteAllRows
Else
SuggestionsList.DeleteAllRows
ResultList.DeleteAllRows
If Me.Text <> "" Then Completer.QueryFragment = Me.Text
End If
End EventHandler
End Control
Control SuggestionsList Inherits Listbox
ControlInstance SuggestionsList Inherits Listbox
EventHandler Sub DoubleClick()
If Me.ListIndex >=0 Then
ResultList.DeleteAllRows
Dim SuggestedCompletedString As MKLocalSearchCompletionMBS = Me.RowTag(Me.ListIndex)
SearchFor( SuggestedCompletedString )
end if
End EventHandler
End Control
Control ResultList Inherits Listbox
ControlInstance ResultList Inherits Listbox
EventHandler Sub DoubleClick()
If Me.ListIndex >=0 Then
// remove the old annotations
MapView.removeAnnotations( MapView.annotations )
Dim mapItem As MKMapItemMBS = MKMapItemMBS( Me.RowTag( Me.ListIndex ) )
if MapView <> nil then
// a zoom factor of 17 seems the most appropriate to me
MapView.setCenterCoordinate( mapItem.placemark.Coordinate, 17, True )
// and as we have access to the coordinates we can stick a pin in it
Dim pin As New MKPointAnnotationMBS
pin.coordinate = mapItem.placemark.Coordinate
pin.title = mapItem.Name
mapView.addAnnotation pin
End If
End If
End EventHandler
End Control
Control ErrorLabel Inherits Label
ControlInstance ErrorLabel Inherits Label
End Control
Control cbStartSearchWithReturnOnly Inherits CheckBox
ControlInstance cbStartSearchWithReturnOnly Inherits CheckBox
EventHandler Sub Action()
'
' If Me.Value Then
'
' SuggestionsList.DeleteAllRows
' ResultList.DeleteAllRows
'
' End If
StartSearchWithReturnKeyOnly = Me.Value
End EventHandler
End Control
Control Map Inherits MapKitViewControlMBS
ControlInstance Map Inherits MapKitViewControlMBS
EventHandler Sub Open()
Self.mapview = Me.View
Me.view.showsScale = True
Me.view.showsZoomControls = True
Me.view.ShowsCompass = True
Me.view.showsPointsOfInterest = True
Me.view.showsBuildings = True
End EventHandler
End Control
Control SegmentedControl1 Inherits SegmentedControl
ControlInstance SegmentedControl1 Inherits SegmentedControl
EventHandler Sub Action(itemIndex as integer)
mapView.mapType = itemIndex
If itemIndex = 3 Then // flyover
// addjust camera
Dim mapCamera As MKMapCameraMBS = mapview.camera
mapCamera.pitch = 45
mapCamera.altitude = 500 // example altitude
mapCamera.heading = 45
End If
End EventHandler
EventHandler Sub Open()
me.Items(0).selected = true
End EventHandler
End Control
EventHandler Sub Open()
Completer = new MKLocalSearchCompleter
completer.SuggestionsList = SuggestionsList
Completer.ErrorLabel = ErrorLabel
End EventHandler
Sub SearchFor(SuggestedCompletedString as MKLocalSearchCompletionMBS)
If LocalSearch <> Nil Then
LocalSearch.cancel
end if
Dim request As New MKLocalSearchRequestMBS( SuggestedCompletedString )
' ResultList.DeleteAllRows
LocalSearch = New MKLocalSearch(request)
LocalSearch.ResultList = ResultList
LocalSearch.Start
End Sub
Property Completer As MKLocalSearchCompleter
Property LocalSearch As MKLocalSearch
Property MapView As MKMapViewMBS
Property StartSearchWithReturnKeyOnly As Boolean = false
End Class
Class MKLocalSearch Inherits MKLocalSearchMBS
EventHandler Sub SearchFinished(response as MKLocalSearchResponseMBS, error as NSErrorMBS, tag as Variant)
#Pragma unused tag
If response <> Nil Then
Dim mapItems() As MKMapItemMBS = response.mapItems
' ResultList.DeleteAllRows
For Each mapItem As MKMapItemMBS In mapItems
Dim location As CLLocationMBS = mapItem.placemark.location
ResultList.AddRow mapItem.Name, Str(location.Latitude)+"/"+Str(location.longitude)
ResultList.RowTag( ResultList.LastIndex ) = mapItem
Dim p As MKPlacemarkMBS = mapItem.placemark
ResultList.AddRow "Placemark found:"
ResultList.AddRow " name: ", p.name
ResultList.AddRow " locality: ", p.locality
ResultList.AddRow " subLocality: ", p.subLocality
ResultList.AddRow " thoroughfare: ", p.thoroughfare
ResultList.AddRow " subThoroughfare: ", p.subThoroughfare
For Each a As String In p.areasOfInterest
ResultList.AddRow " areasOfInterest: ", a
Next
ResultList.AddRow " inlandWater: ", p.inlandWater
ResultList.AddRow " ocean: ", p.ocean
ResultList.AddRow " postalCode: ", p.postalCode
ResultList.AddRow " administrativeArea: ", p.administrativeArea
ResultList.AddRow " subAdministrativeArea: ", p.subAdministrativeArea
ResultList.AddRow " ISOcountryCode: ", p.ISOcountryCode
ResultList.AddRow " country: ", p.country
Dim cl As CLLocationMBS = p.location
If cl<>Nil Then
ResultList.AddRow " latitude: ",CStr(cl.latitude)
ResultList.AddRow " longitude: ",CStr(cl.longitude)
End If
next
End If
If error <> Nil Then
MsgBox error.LocalizedDescription
End If
End EventHandler
Property ResultList As listbox
End Class
Class MKLocalSearchCompleter Inherits MKLocalSearchCompleterMBS
EventHandler Sub DidFailWithError(error as NSErrorMBS)
ErrorLabel.text = error.LocalizedDescription
End EventHandler
EventHandler Sub DidUpdateResults()
Dim results() As MKLocalSearchCompletionMBS = Self.results
' SuggestionsList.DeleteAllRows
If results <> Nil Then
For Each item As MKLocalSearchCompletionMBS In results
SuggestionsList.AddRow item.title, item.subtitle
SuggestionsList.RowTag(SuggestionsList.LastIndex) = item
Next
End If
ErrorLabel.text = ""
End EventHandler
Property ErrorLabel As label
Property SuggestionsList As listbox
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacFrameworks Plugin.