google-maps-geocoding-0.2.0.0: Google Maps Geocoding API bindings

Copyright(c) Mike Pilgrem 2017
Maintainerpublic@pilgrem.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Web.Google.Maps.Geocoding

Contents

Description

This package has no connection with Google Inc. or its affiliates.

The Google Maps Geocoding API provides a direct way to access geocoding and reverse geocoding services via an HTTP request. This library provides bindings in Haskell to that API.

NB: The use of the Google Maps Geocoding API services is subject to the Google Maps APIs Terms of Service, which terms restrict the use of content (eg no use without a Google map).

The components and optional parameters in a geocoding request are not yet implemented. The reverse geocoding request is not yet implemented.

The code below is an example console application to test privately the use of the library with the Google Maps Geocoding API.

{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Data.Maybe (fromJust)
import Data.Text (Text)
import Data.Text.IO as T (getLine, putStr)
import Graphics.Gloss (Display (..), display, white)
import Graphics.Gloss.Juicy (fromDynamicImage)
import Network.HTTP.Client (Manager, newManager)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Web.Google.Maps.Geocoding (Address (..), geocode, GeocodingResponse (..),
    Geometry (..), Key (..), Location (..), Result (..), Status (..))
import Web.Google.Static.Maps (Center (..), Size (..), staticmap, Zoom (..))  
import System.IO (hFlush, stdout)

main :: IO ()
main = do
    putStrLn "A test of the Google Maps Geocoding API.\nNB: The use of the \
        \API services is subject to the Google Maps APIs Terms of Serivce at \
        \https://developers.google.com/maps/terms.\n"
    txt <- input "Enter full address: "
    mgr <- newManager tlsManagerSettings
    let apiKey = Key "<REPLACE_THIS_WITH_YOUR_ACTUAL_GOOGLE_API_KEY>"
    result <- geocode mgr apiKey (Address txt)
    case result of
        Right response -> do
            let s = status response
            case s of
                OK -> do
                    let center = Center $ location $ geometry $ head $
                            results response
                    print center
                    displayMap mgr apiKey center
                _  -> putStrLn $ "Error! Status: " ++ show s
        _ -> putStrLn $ "Error! Result:\n" ++ show result

input :: Text -> IO Text
input msg = T.putStr msg >> hFlush stdout >> T.getLine

displayMap :: Manager -> Key -> Center -> IO ()
displayMap mgr apiKey center = do
    let zoom = Just $ Zoom 17
        w    = 400
        h    = 400
        size = Size w h
    result <- staticmap mgr apiKey Nothing (Just center) zoom size Nothing
        Nothing [] Nothing [] [] Nothing
    case result of
        Right response -> do
            let picture = fromJust $ fromDynamicImage response
                title   = "Test Google Maps Geocoding API"
                window  = InWindow title (w, h) (10, 10)
            display window white picture
        Left err -> putStrLn $ "Error while displaying map: " ++ show err

Synopsis

Functions

geocode :: Manager -> Key -> Address -> IO (Either ServantError GeocodingResponse) Source #

Geocode. NB: The use of the Google Maps Geocoding API services is subject to the Google Maps APIs Terms of Service.

API

type GoogleMapsGeocodingAPI = "geocode" :> ("json" :> (QueryParam "key" Key :> (QueryParam "address" Address :> Get '[JSON] GeocodingResponse))) Source #

Google Translate API

Types

newtype Key :: * #

API key

Constructors

Key Text 

Instances

Eq Key 

Methods

(==) :: Key -> Key -> Bool #

(/=) :: Key -> Key -> Bool #

Show Key 

Methods

showsPrec :: Int -> Key -> ShowS #

show :: Key -> String #

showList :: [Key] -> ShowS #

ToHttpApiData Key 

data Status Source #

Contains the status of the request and may contain debugging information to help you track down why geocoding is not working.

Constructors

OK

Indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.

ZeroResults

Indicates that the geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address.

OverQueryLimit 
RequestDenied 
InvalidRequest

Generally indicates that the query (address, components or latlng) is missing.

UnknownError 

data Result Source #

A result of the geocoder.

Instances

Eq Result Source # 

Methods

(==) :: Result -> Result -> Bool #

(/=) :: Result -> Result -> Bool #

Show Result Source # 
Generic Result Source # 

Associated Types

type Rep Result :: * -> * #

Methods

from :: Result -> Rep Result x #

to :: Rep Result x -> Result #

FromJSON Result Source # 
type Rep Result Source # 

data AddressType Source #

Address (and address component) type: The list of types provided by Google (as at 4 March 2017) is incomplete.

Constructors

AddressType Text 

Instances

newtype PostcodeLocality Source #

Postcode locality: a locality contained in a postal code.

Constructors

PostcodeLocality Text 

newtype PlaceId Source #

Place id

Constructors

PlaceId Text 

Instances

Eq PlaceId Source # 

Methods

(==) :: PlaceId -> PlaceId -> Bool #

(/=) :: PlaceId -> PlaceId -> Bool #

Show PlaceId Source # 
Generic PlaceId Source # 

Associated Types

type Rep PlaceId :: * -> * #

Methods

from :: PlaceId -> Rep PlaceId x #

to :: Rep PlaceId x -> PlaceId #

FromJSON PlaceId Source # 
type Rep PlaceId Source # 
type Rep PlaceId = D1 (MetaData "PlaceId" "Web.Google.Maps.Geocoding" "google-maps-geocoding-0.2.0.0-8i6HX3KtZDy3wOWJfE0Edz" True) (C1 (MetaCons "PlaceId" PrefixI False) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Text)))

data Location :: * #

Location: precision in latitude or longitude beyond 6 decimal places is ignored.

Constructors

Location 

Fields

  • lat :: Double

    Takes any value between -90 and 90.

  • lng :: Double

    Takes any value between -180 and 180.

data Viewport Source #

Viewport

Constructors

Viewport 

Instances

Eq Viewport Source # 
Show Viewport Source # 
Generic Viewport Source # 

Associated Types

type Rep Viewport :: * -> * #

Methods

from :: Viewport -> Rep Viewport x #

to :: Rep Viewport x -> Viewport #

FromJSON Viewport Source # 
type Rep Viewport Source # 
type Rep Viewport = D1 (MetaData "Viewport" "Web.Google.Maps.Geocoding" "google-maps-geocoding-0.2.0.0-8i6HX3KtZDy3wOWJfE0Edz" False) (C1 (MetaCons "Viewport" PrefixI True) ((:*:) (S1 (MetaSel (Just Symbol "southwest") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Location)) (S1 (MetaSel (Just Symbol "northeast") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Location))))