Skip to content

Instantly share code, notes, and snippets.

@never615
Created March 3, 2016 04:05
Show Gist options
  • Save never615/635742ae910547e58a43 to your computer and use it in GitHub Desktop.
Save never615/635742ae910547e58a43 to your computer and use it in GitHub Desktop.
mapbox坐标转换代码
package com.mallto.mt_lbs.geometry;
import android.graphics.PointF;
import com.mallto.mt_lbs.constants.GeoConstants;
/**
* Created by never615 on 3/3/16.
*/
interface GeoConstantstest {
// http://en.wikipedia.org/wiki/Earth_radius#Equatorial_radius
int RADIUS_EARTH_METERS = 6378137; //metre
double MIN_LATITUDE = -85.05112878;
double MAX_LATITUDE = 85.05112878;
double MIN_LONGITUDE = -180;
double MAX_LONGITUDE = 180;
double EARTHSIZE = (RADIUS_EARTH_METERS * Math.PI * 2); //地球周长
}
public class test implements GeoConstantstest{
/**
* Converts a pixel from pixel XY coordinates at a specified level of detail into
* latitude/longitude WGS-84 coordinates (in degrees).
*
* @param pixelX X coordinate of the point, in pixels
* @param pixelY Y coordinate of the point, in pixels
* @param levelOfDetail Level of detail, from 1 (lowest detail) to 23 (highest detail)
* @return Output parameter receiving the latitude and longitude in degrees.
*/
public static LatLng pixelXYToLatLong(double pixelX, double pixelY, final float levelOfDetail) {
final double mapSize = mapSize(levelOfDetail);
final double maxSize = mapSize - 1.0;
double x = wrap(pixelX, 0, maxSize, mapSize);
double y = wrap(pixelY, 0, maxSize, mapSize);
x = (clip(x, 0, maxSize) / mapSize) - 0.5;
y = 0.5 - (clip(y, 0, maxSize) / mapSize);
final double latitude = 90.0 - 360.0 * Math.atan(Math.exp(-y * 2 * Math.PI)) / Math.PI;
final double longitude = 360.0 * x;
return new LatLng(latitude, longitude);
}
/**
* Converts a point from latitude/longitude WGS-84 coordinates (in degrees) into pixel XY
* coordinates at a specified level of detail.
*
* @param latitude Latitude of the point, in degrees
* @param longitude Longitude of the point, in degrees
* @param levelOfDetail Level of detail, from 1 (lowest detail) to 23 (highest detail)
* @param reuse An optional Point to be recycled, or null to create a new one automatically
* @return Output parameter receiving the X and Y coordinates in pixels
*/
public static PointF latLongToPixelXY(double latitude, double longitude,
final float levelOfDetail, final PointF reuse) {
latitude = wrap(latitude, -90, 90, 180);
longitude = wrap(longitude, -180, 180, 360);
final PointF out = (reuse == null ? new PointF() : reuse);
latitude = clip(latitude, MIN_LATITUDE, MAX_LATITUDE);
longitude = clip(longitude, MIN_LONGITUDE, MAX_LONGITUDE);
final double x = (longitude + 180) / 360;
final double sinLatitude = Math.sin(latitude * Math.PI / 180);
final double y = 0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);
final float mapSize = mapSize(levelOfDetail);
out.x = (float) clip(x * mapSize, 0, mapSize - 1);
out.y = (float) clip(y * mapSize, 0, mapSize - 1);
return out;
}
/**
* Clips a number to the specified minimum and maximum values.
*
* @param n The number to clip
* @param minValue Minimum allowable value
* @param maxValue Maximum allowable value
* @return The clipped value.
*/
private static double clip(final double n, final double minValue, final double maxValue) {
return Math.min(Math.max(n, minValue), maxValue);
}
/**
* Returns a value that lies within <code>minValue</code> and <code>maxValue</code> by
* subtracting/adding <code>interval</code>.
*
* @param n the input number
* @param minValue the minimum value
* @param maxValue the maximum value
* @param interval the interval length
* @return a value that lies within <code>minValue</code> and <code>maxValue</code> by
* subtracting/adding <code>interval</code>
*/
private static double wrap(double n, final double minValue, final double maxValue,
final double interval) {
if (minValue > maxValue) {
throw new IllegalArgumentException(
"minValue must be smaller than maxValue: " + minValue + ">" + maxValue);
}
if (interval > maxValue - minValue + 1) {
throw new IllegalArgumentException(
"interval must be equal or smaller than maxValue-minValue: "
+ "min: "
+ minValue
+ " max:"
+ maxValue
+ " int:"
+ interval
);
}
while (n < minValue) {
n += interval;
}
while (n > maxValue) {
n -= interval;
}
return n;
}
protected static int mTileSize = 256; //默认值,可以设置
/**
* Determines the map width and height (in pixels) at a specified level of detail.
*
* @param levelOfDetail Level of detail, from 1 (lowest detail) to 23 (highest detail)
* @return The map width and height in pixels
*/
public static int mapSize(final float levelOfDetail) {
return (int) (leftShift(mTileSize, levelOfDetail));
}
/**
* simulate a binary left shift of a number without using bit operations.
* @param value
* @param multiplier
* @return
*/
public static float leftShift(final float value, final float multiplier) {
return (float) (value * Math.pow(2, multiplier));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment