Search in sources :

Example 1 with AddressComponent

use of org.structr.common.geo.AddressComponent in project structr by structr.

the class GeocodeFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (arrayHasLengthAndAllElementsNotNull(sources, 3)) {
            final String street = sources[0].toString();
            final String city = sources[1].toString();
            final String country = sources[2].toString();
            final GeoCodingResult result = GeoHelper.geocode(street, null, null, city, null, country);
            if (result != null) {
                final Map<String, Object> map = new LinkedHashMap<>();
                map.put("latitude", result.getLatitude());
                map.put("longitude", result.getLongitude());
                AddressComponent cur = null;
                cur = result.getAddressComponent(GeoCodingResult.Type.country);
                if (cur != null) {
                    map.put("country", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.postal_code);
                if (cur != null) {
                    map.put("postalCode", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.locality);
                if (cur != null) {
                    map.put("city", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.route);
                if (cur != null) {
                    map.put("street", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.street_number);
                if (cur != null) {
                    map.put("houseNumber", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.administrative_area_level_1);
                if (cur != null) {
                    map.put("state", cur.getValue());
                }
                cur = result.getAddressComponent(GeoCodingResult.Type.administrative_area_level_3);
                if (cur != null) {
                    map.put("stateDistrict", cur.getValue());
                }
                return map;
            }
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : GeoCodingResult(org.structr.common.geo.GeoCodingResult) AddressComponent(org.structr.common.geo.AddressComponent) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with AddressComponent

use of org.structr.common.geo.AddressComponent in project structr by structr.

the class TestNine method geocode.

public void geocode() throws FrameworkException {
    Double lat = getProperty(latitude);
    Double lon = getProperty(longitude);
    if (lat == null || lon == null) {
        String _city = getProperty(city);
        String _street = getProperty(street);
        String _postalCode = getProperty(postalCode);
        GeoCodingResult geoCodingResult = GeoHelper.geocode(_street, null, _postalCode, _city, null, null);
        if (geoCodingResult == null) {
            return;
        }
        setProperty(latitude, geoCodingResult.getLatitude());
        setProperty(longitude, geoCodingResult.getLongitude());
        // set postal code if found
        AddressComponent postalCodeComponent = geoCodingResult.getAddressComponent(GeoCodingResult.Type.postal_code);
        if (postalCodeComponent != null) {
            setProperty(postalCode, postalCodeComponent.getValue());
        }
    }
}
Also used : GeoCodingResult(org.structr.common.geo.GeoCodingResult) AddressComponent(org.structr.common.geo.AddressComponent)

Aggregations

AddressComponent (org.structr.common.geo.AddressComponent)2 GeoCodingResult (org.structr.common.geo.GeoCodingResult)2 LinkedHashMap (java.util.LinkedHashMap)1