tcacciatore
tcacciatore

Reputation: 493

High coordinates given by an api

in my Android app, I need to display some points on a map. I get points coordinates from an API (http://data.citedia.com/r1/parks).

However, coordinated given by the API have high values : "geometry":{"type":"Point","coordinates":[-187178.15,6124340.9]}. I mean longitude is between -180 or 180...

Has anyone experienced the same issue ?

Upvotes: 1

Views: 88

Answers (1)

N Dorigatti
N Dorigatti

Reputation: 3540

Ok, so here it is how i did it: download the JavaProj-noawt library (from http://www.java2s.com/Code/Jar/j/Downloadjavaproj106noawtjar.htm ) or any other source, it's a version suitable for android, which does not have awt classes.

then use the code below:

public static double[] convertFromWebMercatorToLatLng(double x, double y) {
    Projection googleProjection = getGoogleProjection();
    Point2D.Double latLngPoint = new Point2D.Double();
    latLngPoint = googleProjection.inverseTransform(new Point2D.Double(x, y), latLngPoint);
    return new double[] { latLngPoint.x, latLngPoint.y };
}

private static final int mGoogleEPSG = 3785;
// Projection EPSG
private static Projection mGoogleProjectionSystem = null;
private static Projection getGoogleProjection() {
    if (null == mGoogleProjectionSystem) {
        mGoogleProjectionSystem = ProjectionFactory.getNamedPROJ4CoordinateSystem("epsg:" + mGoogleEPSG);
    }
    return mGoogleProjectionSystem;
}

You just put this code in a "Util" class and the call it as a static method providing x(horizontal) and y(vertical) coordinates, will give you an array having in '0' the x (longitude) and in '1' latitude.

Be aware that LatLng object in google maps is "switched", containing (y,x) instead of (x,y) as in the methods above!

Upvotes: 2

Related Questions