2015-03-31 78 views
3

您好我正在將應用程序從Google地圖移植到Skmaps。一個API需要東北和西南的點,但在Skmaps中我找不到獲得這些點的方法。我嘗試使用onMapRegionChangeEnded接口方法,但只返回地圖的中心點和縮放級別。從skmaps獲取東北和西南

怎麼能得到它?

+0

你需要根據東北以定向地圖軸? (我不確定你的意思是「東北角」 – Ando 2015-04-01 07:46:20

+0

我需要將右上角像素和左下角像素轉換爲屏幕中的東北和西南座標,以獲得一個矩形以發送到返回此區域內標記的API 。 – ClarkXP 2015-04-01 13:08:32

回答

2

我做了下一個基於C#代碼的解決方法來從Google的靜態地圖中獲取邊界。然而,我必須改善縮放範圍才能使用skmaps。

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); 
    } 


} 

Mapbound類:

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(); 



}