2

I'm using swift MapKit MKLocalSearch. It returns an array of MKLocalSearchCompletion. However, MKLocalSearchCompletion only exposes title and subtitle fields. I want to extract the City and State from the address, but simply deconstructing the subtitle doesn't work since it has an unreliable format (sometimes the city is the 2nd or 3rd component, if you split it by ",", and sometimes it is not there - only a postal code. I used AppCode's debugger and found that the information that I want - neatly strcutured - is actually potentially available but it is buried deep down in MKLocalSearchCompletion, and it isn't accessible (it is private)... The type is GEOStructuredAddress and it has all the fields returned by MapKit. Here is how it is buried in MKLocalSearchCompletion (where self = MKLocalSearchCompletion)

(((((self.addressResults[0]._mapItem as! MKMapItem *)._geoMapItem as! _GEOPlaceDataItem *)._place as! GEOPlace *)._address as! GEOAddress *)._structuredAddress as! GEOStructuredAddress *)._administrativeArea

What can I do to get this info? Is there a diffrent way to use MapKit to make a query which will give me more control of the response?

I already saw https://stackoverflow.com/a/41718320/5683904 but I'm not sure how to use it with MKLocalSearchCompleterDelegate. (I assume that I'm going to need to use MKLocalSearch and create my own "completer" - is this the only way?)

akraines
  • 1,965
  • 2
  • 12
  • 27
  • 1
    You can get information of such coordinates by `CLGeocoder` – Abdulaziz.Musaileekh Sep 05 '18 at 16:39
  • But I don't have access to the co-ordinates of the MKLocalSearchCompletion results. How do convert the digest information provided by subtitle to CLGeocoder to get the information I want? – akraines Sep 05 '18 at 16:41
  • Can you please debug the response of `MKLocalSearchCompletion` you will get `MKMapItem` as a dictionary of several values you need – Abdulaziz.Musaileekh Sep 05 '18 at 16:45
  • That is exactly the source of (((((self.addressResults[0]._mapItem as! MKMapItem *)._geoMapItem as! _GEOPlaceDataItem *)._place as! GEOPlace *)._address as! GEOAddress *)._structuredAddress as! GEOStructuredAddress *)._administrativeArea that I wrote above. The problem is that I couldn't access it because it isn't exposed by MKLocalSearchCompletion... I couldn't even evaluate the expression in AppCode's debugger... If you can show me how then that would be the answer I'm looking for. Thanks – akraines Sep 05 '18 at 16:51
  • ` { isCurrentLocation = 0; name = "Merriweather Coffee + Kitchen"; phoneNumber = "\U202d+1 (646) 678-5678\U202c"; placemark = "Merriweather Coffee + Kitchen, 428 Hudson St, New York, NY 10014, United States @ <+40.73055800,-74.00649100> +/- 0.00m, region CLCircularRegion (identifier:'<+40.73055801,-74.00649100> radius 141.17', center:<+40.73055801,-74.00649100>, radius:141.17m)"; timeZone = "America/New_York (EDT) offset -14400 (Daylight)"; url = "http://www.merriweathernyc.com"; }` this what you are looking for? – Abdulaziz.Musaileekh Sep 05 '18 at 17:28
  • I'm looking for this: { administrativeArea = "New York"; administrativeAreaCode = NY; areaOfInterest = ( "Long Island", "Farmingdale State University of New York" ); country = "United States"; countryCode = US; dependentLocality = ( Farmingdale ); fullThoroughfare = "2350 Broadhollow Rd"; geoId = ( ); locality = Farmingdale; postCode = 11735; subAdministrativeArea = Suffolk; subThoroughfare = 2350; thoroughfare = "Broadhollow Rd"; } – akraines Sep 05 '18 at 18:31
  • In the first item, you will find coordinates to use with CLGeocoder to get the required information – Abdulaziz.Musaileekh Sep 05 '18 at 18:49
  • My issue is coercing MKLocalSearchCompletion to MKMapItem. I'll test tyat and see if it works. Thanks – akraines Sep 06 '18 at 10:11

0 Answers0