% -*- mode: latex; -*- mustache tags:  
\ifx\wholebook\relax\else

	\documentclass[10pt,twoside,english]{root/support/latex/sbabook/sbabook}
  \usepackage{import}
  \subimport{root/support/latex/}{common.tex}

  \hypersetup{pdfinfo = {
    Title = {{Pharo Mooc}},
    Author = {},
    Keywords = {pharo, smalltalk}}}
  \begin{document}
\fi

\chapter{Challenge 5 - Solution}
The web site \url{http://nominatim.openstreetmap.org/} offers a search service \url{http://nominatim.openstreetmap.org/search/} that when given 
a street name will return occurrences of such street name, each one with characterizing information such as longitude and latitude.
In this challenge, you should write an expression that returns a collection of location using for example a pair whose key is the description
of the location and the value its latitude and longitude.

For example we did a query with Rue de Paris and we selected two results and they are as follows:

\begin{displaycode}{smalltalk}
{
'La Rue de Paris, Lainsecq, Auxerre, Yonne, Bourgogne-Franche-Comte, France metropolitaine, 89520, France'->(47.5612874@3.2933713) .
'Rue de Paris, Boulogne-Billancourt, Hauts-de-Seine, Ile-de-France, France metropolitaine, 92100, France'->(48.8416645@2.236876) .
}
\end{displaycode}
\subsection{Hints: }
\begin{itemize}
\item We suggest to ask the server to return JSON.
\item To specify the format, put 'jsonv2' to 'format' in the query.
\item You can also define a limit to the number of results using the parameter 'limit' and put it to '10'.
\item To parse JSON, have a look at the \textcode{STON} class
\item Use the message \textcode{-\textgreater{}} to build a pair.
\end{itemize}
\subsection{Solution}
\begin{displaycode}{smalltalk}
	| query result entries |
	query := UIManager default request: 'Find location for:'.
	result := STON fromString: 
			 (ZnClient new
			 	url: 'http://nominatim.openstreetmap.org/search/', query;
				queryAt: 'format' put: 'jsonv2';
				queryAt: 'limit' put: '10';
				get;
				contents).
	(result collect: [ :dct |
		(dct at: 'display_name') -> 
			((dct at: 'lat') asNumber @ (dct at: 'lon') asNumber)
			]) inspect
\end{displaycode}


\ifx\wholebook\relax\else
\end{document}
\fi