· 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?


Discussion (5)0
Log in or sign up to continue

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

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 }