! Copyright (C) 2009 Darrin Thompson. ! See http://factorcode.org/license.txt for BSD license. USING: accessors http.client kernel locals math math.order math.parser namespaces sequences sorting.slots splitting urls urls.encoding ; IN: googleapi SYMBOLS: google-maps-api-key ; TUPLE: google-geolocation status accuracy latitude longitude ; C: google-geolocation :: google-geo-locate-url ( address -- url ) URL" http://maps.google.com/maps/geo" [ address "q" set "csv" "output" set "false" "sensor" set google-maps-api-key get "key" set ] H{ } make-assoc >>query ; :: seq>google-geolocation ( input -- geo ) [let | status [ 0 input nth string>number ] accuracy [ 1 input nth string>number ] latitude [ 2 input nth ] longitude [ 3 input nth ] | status accuracy latitude longitude ] ; : acceptable? ( obj -- bool ) accuracy>> 8 >= ; : sort-geo ( arr -- arr ) { { accuracy>> >=< } } sort-by ; : select-best ( arr -- obj/f ) [ acceptable? ] filter sort-geo 0 swap ?nth ; : google-geo-locate ( address -- arr ) google-geo-locate-url http-get nip string-lines [ "," split seq>google-geolocation ] map select-best ;