KatrinaHoffert / EatSafe

An app for finding safe places to eat
Other
2 stars 2 forks source link

Get the coordinate object from a asynchronous result #92

Closed LujieDuan closed 9 years ago

LujieDuan commented 9 years ago

I have worked on this but haven't got progress for days, so if anyone have time please take a look at my code and give me some advice:)

I want to make a http get request in Scala to the following URL: https://maps.googleapis.com/maps/api/geocode/json?address=101+Cumberland,+Saskatoon Which will give me a JSON object contains the lat and lng of this location.

Play framework recommends to handle the request and result as an asynchronous result. So I get

val futureResult : Future[Coordinate] = holder.withQueryString("address" -> parameterString).get().map{ response => (response.json \ "results")(0).as[Coordinate] }

where futureResult is a asynchronous result that contains the coordinate object. This is where I got stuck. How to get the coordinate from this asynchronous result?

The code is in the branch "duan_Handling_asynchronous_results" in app/coordinatesGetter/PopulateCoordinates.scala.

To run the code, type "./activator testOnly RunCoordinate".

KatrinaHoffert commented 9 years ago

Read this for how to use Futures.

Basically, you need to create the callback that is called when the future is completed. That'll have the results of the request.

KatrinaHoffert commented 9 years ago

Also, see here: https://www.playframework.com/documentation/2.0/ScalaWS

LujieDuan commented 9 years ago

Thanks Mike, the links are very helpful. I have got the coordinates out of the future. Thanks for saving my life again! :P