Evgeny Shvarov · Mar 23, 2016

Get coordinates against street address


Does anyone have an example of subj?

I know there is google service for it. Anyone uses it in production? What is the feedback?

In case of positive feedback would you please post the code example?


0 0 5 355
Log in or sign up to continue

We used Google's service for an application for a while but ended up pulling it out.  Let me know if you want me to point you to the specific code.

I use such code in one of my projects

ClassMethod Test()
    set address="One Memorial Drive, Cambridge, MA 02142, USA"
    do ..GetCoords(address, .latitude, .longitude, .partialMatch)
    zw latitude, longitude, partialMatch

ClassMethod GetCoords(Address As %String, Output Latitute As %Float = "", Output Longitude As %Float = "", Output PartialMatch As %Boolean) As %Status
    set params("address")=Address
    set sc=..CallGoogleAPI("/maps/api/geocode/json", .params, .data)
    if data.status="OK" {
        set result=data.results.$get(0)
        set PartialMatch = +result."partial_match"
        set Latitute =
        set Longitude = result.geometry.location.lng
    quit $$$OK

ClassMethod CallGoogleAPI(Url As %String, ByRef Params, Output Data) As %Status
    #;set Params("key")="your api key here"
    quit ..CallApi("", 1, Url, .Params, .Data)

ClassMethod CallApi(Server As %String, Secure As %Boolean = 1, Url As %String, ByRef Params, Output Data) As %Status
    set ht=##class(%Net.HttpRequest).%New()
    set ht.Server=""
    set ht.Https=Secure
    set:Secure ht.SSLConfiguration=..GetSSLCertificate(Server)
    set param=""
    for {
        set param=$order(Params(param),1,value)
        do ht.SetParam(param, value)
    set sc=ht.Get(Url)
    if $$$ISERR(sc) quit sc
    set Data={}
    set Data=Data.$fromJSON(ht.HttpResponse.Data)
    quit $$$OK

ClassMethod GetSSLCertificate(Server As %String) As %Status
    new $namespace
    znspace "%SYS"
    do {
        set tSC=##class(Security.SSLConfigs).Create(Server)
    } while 0
    quit Server

I used the Google Geocode API some while ago and if you call it occasionally you won't see any issues. If you want to bulk load some data and attach lat/long values during that process you have to queue your requests to Google. They only allow 3~4 requests per second for each API key otherwise, you will get a bad response.

Here is a sample method which I created for a Zen Mojo project a couple of years ago:

ClassMethod GeocodeAddress(pAddress As %String, Output pFormattedAddress As %String, Output pLatitude As %Numeric, Output pLongitude As %Numeric) As %Status { 
Set sc=$$$OK Set r=##class(%Net.HttpRequest).%New() 
Set r.Server="" 
Set url="/maps/api/geocode/json?address=" 
Set address=pAddress 
Set requestUrl=url_##class(%CSP.Page).EscapeURL(address) 
Do r.Get(requestUrl) 
Set json=r.HttpResponse.Data.Read() 
Set sc=##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(json,,.geoObj) 
If $$$ISERR(sc) Quit sc 
If (geoObj.status="OK") { 
 Set pFormattedAddress=geoObj.results.GetAt(1)."formatted_address" 
 Set pLatitude=geoObj.results.GetAt(1) 
 Set pLongitude=geoObj.results.GetAt(1).geometry.location.lng 
Else { 
 Set sc=$$$ERROR($$$GeneralError,"Google API Returned the following status: "_geoObj.status) 
Quit sc }