0

When I do println(localSearchResponse), I get a MapItem object, which includes a ton of details about the location. In this example, its UCSD. Here is the output showing in my log.

<MKLocalSearchResponse: 0x1c53d640> {
    boundingRegion = "<center:+32.87514836, -117.23958822 span:+0.00725621, +0.00825332>";
    mapItems =     (
        "<MKMapItem: 0x1c538090> {\n    isCurrentLocation = 0;\n    name = \"University of California, San Diego\";\n    phoneNumber = \"+18585342230\";\n    placemark = \"University of California, San Diego, 9500 Gilman Dr, La Jolla, CA  92093-5004, United States @ <+32.87529400,-117.23961000> +/- 0.00m, region CLCircularRegion (identifier:'<+32.87514837,-117.23958825> radius 557.57', center:<+32.87514837,-117.23958825>, radius:557.57m)\";\n    url = \"http://www.ucsd.edu\";\n}"
    );
}

Notice how it outputs placemark = University of California... and has the address? How do I get this value and store it into a variable? Here is my code:

localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = addressTextField.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

    if localSearchResponse == nil{
        var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
        alert.show()
        return
    }

    //prints the MKLocalSearchResponse with name, phoneNumber, placemark
    println(localSearchResponse)

    //Get latitude and longitude
    var newRecordLat = localSearchResponse.boundingRegion.center.latitude
    var newRecordLong = localSearchResponse.boundingRegion.center.longitude

    //How do I get the address, which is "placemark" in the MKLocalSearchResponse?
    var newRecordAddress = localSearchResponse.mapItems...???

    //store values to Parse
    self.latToParse = newRecordLat
    self.longToParse = newRecordLong
}

Here is the documentation of MKSearchResponse

And here is the documentation of MKMapItem

Brian
  • 14,610
  • 7
  • 35
  • 43
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

2 Answers2

1

The answer is:

var newRecordAddress = (localSearchResponse.mapItems[0] as! MKMapItem).placemark

This object contains all information you need. Checked it in demo project

Address only:

var newRecordAddress = (localSearchResponse.mapItems[0] as! MKMapItem).placemark
let addressOnly = newRecordAddress.name + ", " + newRecordAddress.title

newRecordAddress.name is place's name newRecordAddress.title is place's address you required

maxpovver
  • 1,580
  • 14
  • 25
  • Thank you. It returns: "5190 Leo St, 5190 Leo St, San Diego, CA 92115-1535, United States @ <+32.77302200,-117.05744500> +/- 0.00m, region CLCircularRegion (identifier:'<+32.77301170,-117.05740795> radius 18.87', center:<+32.77301170,-117.05740795>, radius:18.87m)". Do you know how to get the address only? Or, "5190 Leo St, San Diego, Ca 92115-1535"? – Josh O'Connor Jul 20 '15 at 22:00
  • If you can answer that I will reward for bounty. – Josh O'Connor Jul 20 '15 at 22:01
  • I can do it tomorrow morning – maxpovver Jul 21 '15 at 15:25
  • THANK YOU. This is a crucial aspect to my project, and was worried I had to find another way around this, until you saved the say. Thank you Max Power! – Josh O'Connor Jul 21 '15 at 21:55
  • btw bounty is still not given :) – maxpovver Jul 22 '15 at 10:01
  • sorry! I am new to stack, I thought if I checked it as correct it was given. Did you receive it? – Josh O'Connor Jul 22 '15 at 19:37
0

Because mapItems is an array, you need to call first to access the first element of that array. This will return an MKMapItem, so you can get the placemark property with this code:

localSearchResponse.mapItems.first.placemark
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • Hi Glorfindel. I tried doing that, and came up with an error: 'placemark' is unavailable: APIs deprecated as of ios7 and earlier are unavailable in Swift – Josh O'Connor Jul 18 '15 at 21:08
  • Here is the answer: http://stackoverflow.com/questions/31488686/accessing-mklocalsearchresponse-item-swift – Josh O'Connor Jul 23 '15 at 07:04