Skip to main content
1-Visitor
July 9, 2014
Solved

Location Geocode

  • July 9, 2014
  • 1 reply
  • 4201 views

I mapping all the organizations in or sandbox.

Here is a snippet of the code:

def organizationBridge = Bridges.getOrganizationBridge();

def locationBridge = Bridges.getLocationBridge();

def orgResults = organizationBridge.find(new OrganizationCriteria());

def orgs = orgResults.getOrganizations();

def locations;

def locationRef;

def location;

try{

    orgs.each() {

        locations = it.getLocations();

        locSize = locations.size() ?: 0;

        if (locSize > 0){

            locationRef = locations.get(0);

            location = locationBridge.findById(locationRef.getSystemId());

            geocode = location.getGeocode() ?: "0,0";

            geocodeList = Arrays.asList(geocode.split("\\s*,\\s*"));

            row.put("Latitude", geocodeList.get(0));

            row.put("Longitude", geocodeList.get(1));

        }

        rows.push(row);

        row = new HashMap();

    }

}catch(Exception e)

{

    bSuccess = false;

}

 

Sometime getGeocode() returns a null.

How does the system know the geocode of the address?

Is there a way to refresh or update the geocodes?

Thanks

Jay

Best answer by jw614h

I figured out how to accomplish this from the API doc

You need to pass in the Organization ID(orgId) and the new geocode(newgeocode as lat,long) :


import groovy.json.JsonBuilder;


import groovy.json.JsonSlurper;


import com.axeda.sdk.v2.dsl.Bridges;


import com.axeda.sdk.v2.bridge.LocationBridge;


import com.axeda.services.v2.LocationCriteria;



HashMap root = new HashMap();


List rows = new ArrayList();


HashMap row = new HashMap();



def organizationBridge = Bridges.getOrganizationBridge();


def org = organizationBridge.findById(parameters.orgId);


def locationBridge = Bridges.getLocationBridge();


locations = org.getLocations();


locSize = locations.size() ?: 0;


row.put("ClientId", org.getSystemId());


row.put("ClientName", org.getLabel());


if (locSize > 0){


    locationRef = locations.get(0);


    location = locationBridge.findById(locationRef.getSystemId());


    geocode = location.getGeocode() ?: "0,0";


    geocodeList = Arrays.asList(geocode.split("\\s*,\\s*"));


    if (geocodeList.get(0) == "0.0" ){


        row.put("update", true);


        location.setGeocode(parameters.newgeocode);


        locationBridge.update(location);


    }


}


root.put("success", true);


root.put("Client", row);


JsonBuilder builder = new JsonBuilder(root);


return ["Content-Type": "application/javascript","Content":builder.toPrettyString()]


1 reply

5-Regular Member
July 15, 2014

First - if you import the Bridges object statically you can stop def'ing the bridges.  I always start my v2 scripts like this:


import static com.axeda.sdk.v2.dsl.Bridges.*


Then you can simply call 'assetBridge' or 'modelBridge' or whatever bridge you're looking for. I would replace this:


def organizationBridge = Bridges.getOrganizationBridge();


def locationBridge = Bridges.getLocationBridge();


with just that import - it'll work just the same!

As for the .getGeocode() - that *ever* works?  I looked at the object and I don't see a method called getGeocode on the v2 location object at all

jw614h1-VisitorAuthor
1-Visitor
July 15, 2014

It's in the documentation:

https://mentor.axeda.com/javadoc/6_6-SDK_Javadoc/com/axeda/services/v2/Location.html

String getGeocode()

Gets the value of the geocode property.

It brings back comma separated Lat and Long.

It works for some of the locations but not all.

Jay

5-Regular Member
July 15, 2014

Oh geesh I was looking at the wrong version's javadoc/object. My apologies. There is a periodic task that attempts to do this. It's configured to run on the hour by default but this setting can be modified. Assuming you are hosted by Axeda it would probably be best to check with support to ensure that this task is enabled and what the period is your platform is set to run at.

If that task fails for any reason (maybe the service we use can't find the address etc.) it will leave the address at 0,0.  I would probably recommend you contact support with an address that worked and one that didn't (or many of such addresses) and go from there.