android - 从 skmaps 获取东北和西南

标签 android skmaps

您好,我正在将一个应用程序从 Google map 移植到 Skmaps。 API 需要东北点和西南点,但在 Skmaps 中我找不到获取这些点的方法。我尝试使用 onMapRegionChangeEnded 接口(interface)方法,但只返回 map 的中心点和缩放级别。

如何获取?

最佳答案

我根据 C# 代码执行了下一个解决方法,以从 Google 的静态 map 获取边界。然而,我必须改进缩放范围才能使用 skmap。

public class MapsUtils {

    public static int TileSize = 256;
    public static double OriginX, OriginY;
    public static double PixelsPerLonDegree;
    public static double PixelsPerLonRadian;

    public static void MapsUtils() {
        OriginX = TileSize / 2;
        OriginY = TileSize / 2;
        PixelsPerLonDegree = TileSize / 360.0;
        PixelsPerLonRadian = TileSize / (2 * Math.PI);
    }

    public static double DegreesToRadians(double deg) {
        return deg * Math.PI / 180.0;
    }

    public static double RadiansToDegrees(double rads) {
        return rads * 180.0 / Math.PI;
    }

    public static double Bound(double value, double min, double max) {
        value = Math.min(value, max);
        return Math.max(value, min);
    }

    //From Lat, Lon to World Coordinate X, Y. I'm being explicit in assigning to
//X and Y properties.
    public static Coordinate Mercator(double latitude, double longitude) {
        double siny = Bound(Math.sin(DegreesToRadians(latitude)), -.9999, .9999);

        Coordinate c = new Coordinate();
        c.setX(OriginX + longitude * PixelsPerLonDegree);
        c.setY(OriginY + .5 * Math.log((1 + siny) / (1 - siny)) * -PixelsPerLonRadian);

        return c;
    }

    //From World Coordinate X, Y to Lat, Lon. I'm being explicit in assigning to
    //Latitude and Longitude properties.
    public static Coordinate InverseMercator(double x, double y) {
        Coordinate c = new Coordinate();

        c.setLongitude((x - OriginX) / PixelsPerLonDegree);
        double latRadians = (y - OriginY) / -PixelsPerLonRadian;
        c.setLatitude(RadiansToDegrees(Math.atan(Math.sinh(latRadians))));

        return c;
    }

    public static MapBound getBound(Coordinate center,int zoom,int mapWidth,int mapHeight){
        MapsUtils.MapsUtils();
        double scale = Math.pow(2, zoom);

        Coordinate centerWorld = Mercator(center.getLatitude(), center.getLongitude());
        Coordinate centerPixel = new Coordinate();
        centerPixel.setX(centerWorld.getX() * scale);
        centerPixel.setY(centerWorld.getY() * scale);

        Coordinate NEPixel = new Coordinate();
        NEPixel.setX(centerPixel.getX() + mapWidth / 2.0);
        NEPixel.setY(centerPixel.getY() - mapHeight / 2.0);

        Coordinate SWPixel = new Coordinate();
        SWPixel.setX(centerPixel.getX() - mapWidth / 2.0);
        SWPixel.setY(centerPixel.getY() + mapHeight / 2.0);

        Coordinate NEWorld = new Coordinate();
        NEWorld.setX(NEPixel.getX() / scale);
        NEWorld.setY(NEPixel.getY() / scale);

        Coordinate SWWorld = new Coordinate();
        SWWorld.setX(SWPixel.getX() / scale);
        SWWorld.setY(SWPixel.getY() / scale);

        Coordinate NELatLon = InverseMercator(NEWorld.getX(), NEWorld.getY());
        Coordinate SWLatLon = InverseMercator(SWWorld.getX(), SWWorld.getY());

        return new MapBound(NELatLon,SWLatLon);
    }


}

map 绑定(bind)类:

public class MapBound {
    private Coordinate northEast, southWest;

    public MapBound(Coordinate northEast, Coordinate southWest) {
        this.northEast = northEast;
        this.southWest = southWest;
    }

    public Coordinate getNorthEast() {
        return northEast;
    }

    public void setNorthEast(Coordinate northEast) {
        this.northEast = northEast;
    }

    public Coordinate getSouthWest() {
        return southWest;
    }

    public void setSouthWest(Coordinate southWest) {
        this.southWest = southWest;
    }
}

和坐标类:

public class Coordinate{
    private double x,y,latitude, longitude;

    public Coordinate() {
        this.x = 0;
        this.y = 0;
    }

    public Coordinate(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

使用此代码:

@Override
public void onMapRegionChangeEnded(SKCoordinateRegion skCoordinateRegion) {
    MapBound mapBound = MapsUtils.getBound(
            new Coordinate(skCoordinateRegion.getCenter().getLatitude(), skCoordinateRegion.getCenter().getLongitude()),
            (int) skCoordinateRegion.getZoomLevel(),
            skMap.getWidth(),
            skMap.getHeight());
    mapBound.getNorthEast().getLatitude();
    mapBound.getNorthEast().getLongitude();
    mapBound.getSouthWest().getLatitude();
    mapBound.getSouthWest().getLongitude();



}

关于android - 从 skmaps 获取东北和西南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29377121/

相关文章:

android - SKMAPS 导航监听器中未调用 OnDestinationReached

java - 适用于 Android/Java 的正确代码设计

安卓 API21 : VoiceInteractionService onReady() not called

java - 为 android 中的所有 Activity 注册监听器的全局类

ios - Skobbler web map 和iOS Sdk搜索结果不同

ios - 如果我在使用 skobbler 导航时缩小,如何停止自动放大

安卓 : Make 2 services communicate

android - 使用 React Native 更改键盘后面的背景颜色?

android - Skobbler Android 3.0.1 requestScreenshot 返回透明空位图

ios - iOS-SKNavigationSettings音频建议,transportMode之间没有区别