get a hash that represents coords of the tile in google aerial - Java java.lang

Java examples for java.lang:Math Geometry

Description

get a hash that represents coords of the tile in google aerial

Demo Code


//package com.java2s;

public class Main {
    /**//from  ww w .  ja  va2 s .  co  m
     * get a hash that represents coords of the tile in google aerial
     * @param lat
     * @param lon
     * @param zoom
     * @return
     */
    public static String getGoogleAerial(double lat, double lon, int zoom) {

        // first convert the lat lon to transverse mercator coordintes.
        if (lon > 180) {
            lon -= 360;
        }

        lon /= 180;

        // convert latitude to a range -1..+1
        lat = Math.log(Math
                .tan((Math.PI / 4) + (0.5 * Math.PI * lat / 180)))
                / Math.PI;

        double tLat = -1;
        double tLon = -1;
        double lonWidth = 2;
        double latHeight = 2;

        StringBuffer keyholeString = new StringBuffer("t");

        for (int i = 0; i < zoom; i++) {
            lonWidth /= 2;
            latHeight /= 2;

            if ((tLat + latHeight) > lat) {
                if ((tLon + lonWidth) > lon) {
                    keyholeString.append('t');
                } else {
                    tLon += lonWidth;
                    keyholeString.append('s');
                }
            } else {
                tLat += latHeight;

                if ((tLon + lonWidth) > lon) {
                    keyholeString.append('q');
                } else {
                    tLon += lonWidth;
                    keyholeString.append('r');
                }
            }
        }

        return keyholeString.toString();
    }
}

Related Tutorials