Question
· Mar 23, 2016

Get coordinates against street address

Hi!

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?

TIA!

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 = result.geometry.location.lat
        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("maps.googleapis.com", 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="maps.googleapis.com"
    set ht.Https=Secure
    set:Secure ht.SSLConfiguration=..GetSSLCertificate(Server)
    
    set param=""
    for {
        set param=$order(Params(param),1,value)
        quit:param=""
        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 {
        quit:##class(Security.SSLConfigs).Exists(Server)
        
        set tSC=##class(Security.SSLConfigs).Create(Server)
        $$$ThrowOnError(tSC)
    } 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="maps.googleapis.com" 
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).geometry.location.lat 
 Set pLongitude=geoObj.results.GetAt(1).geometry.location.lng 
 } 
Else { 
 Set sc=$$$ERROR($$$GeneralError,"Google API Returned the following status: "_geoObj.status) 
 } 
Quit sc }