2013-10-28 145 views
1

我正在使用Google Maps v2來顯示wms中的瓷磚。我提到this 網站。在加載瓷磚的問題,他們加載多次我不知道嗎?任何1可以幫助我。 enter image description hereWMS在Android瓷磚加載問題上

這裏是我的代碼

package com.example.testgooglemaps; 

import android.app.Activity; 
import android.os.Bundle; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.TileOverlayOptions; 
import com.google.android.gms.maps.model.TileProvider; 

public class Lanch extends Activity { 

    // Google Map 
    private GoogleMap googleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_lanch); 

     try { 
      // Loading map 
      initilizeMap(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    /** 
    * function to load map. If map is not created it will create it for you 
    * */ 
    private void initilizeMap() { 
     if (googleMap == null) { 
      googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

      // check if map is created successfully or not 
      if (googleMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     initilizeMap(); 
    } 

    private void setUpMap() { 
     TileProvider wmsTileProvider = TileProviderFactory.getOsgeoWmsTileProvider(); 
     googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(wmsTileProvider)); 

     // to satellite so we can see the WMS overlay. 
     googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } 

} 

TileProvider類...

package com.example.testgooglemaps; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Locale; 

import android.util.Log; 

public class TileProviderFactory { 

    public static WMSTileProvider getOsgeoWmsTileProvider() { 

     final String OSGEO_WMS = "http://localhost/geoserver/magnamaps/wms?service=WMS&version=1.1.0&request=GetMap&layers=magnamaps:bang_apartments&styles=&bbox=%f,%f,%f,%f&width=256&height=256&crs=EPSG:4326&format=image/png&transparent=true"; 

     WMSTileProvider tileProvider = new WMSTileProvider(256, 256) { 

      @Override 
      public synchronized URL getTileUrl(int x, int y, int zoom) { 
       double[] bbox = getBoundingBox(x, y, zoom); 
       String s = String.format(Locale.US, OSGEO_WMS, bbox[MINX], bbox[MINY], bbox[MAXX], bbox[MAXY]); 
       Log.d("WMSDEMO", s); 
       URL url = null; 
       try { 
        url = new URL(s); 
       } catch (MalformedURLException e) { 
        throw new AssertionError(e); 
       } 
       return url; 
      } 
     }; 
     return tileProvider; 
    } 
} 

WMSTileProvider類...

package com.example.testgooglemaps; 

import java.net.URLEncoder; 

import com.google.android.gms.maps.model.UrlTileProvider; 

public abstract class WMSTileProvider extends UrlTileProvider { 

    // Web Mercator n/w corner of the map. 
    private static final double[] TILE_ORIGIN = { -20037508.34789244, 20037508.34789244 }; 
    // array indexes for that data 
    private static final int ORIG_X = 0; 
    private static final int ORIG_Y = 1; // " 

    // Size of square world map in meters, using WebMerc projection. 
    private static final double MAP_SIZE = 20037508.34789244 * 2; 

    // array indexes for array to hold bounding boxes. 
    protected static final int MINX = 0; 
    protected static final int MAXX = 1; 
    protected static final int MINY = 2; 
    protected static final int MAXY = 3; 

    // cql filters 
    private String cqlString = ""; 

    // Construct with tile size in pixels, normally 256, see parent class. 
    public WMSTileProvider(int x, int y) { 
     super(x, y); 
    } 

    protected String getCql() { 
     return URLEncoder.encode(cqlString); 
    } 

    public void setCql(String c) { 
     cqlString = c; 
    } 

    // Return a web Mercator bounding box given tile x/y indexes and a zoom 
    // level. 
    protected double[] getBoundingBox(int x, int y, int zoom) { 
     double tileSize = MAP_SIZE/Math.pow(2, zoom); 
     double minx = TILE_ORIGIN[ORIG_X] + x * tileSize; 
     double maxx = TILE_ORIGIN[ORIG_X] + (x + 1) * tileSize; 
     double miny = TILE_ORIGIN[ORIG_Y] - (y + 1) * tileSize; 
     double maxy = TILE_ORIGIN[ORIG_Y] - y * tileSize; 

     double[] bbox = new double[4]; 
     bbox[MINX] = minx; 
     bbox[MINY] = miny; 
     bbox[MAXX] = maxx; 
     bbox[MAXY] = maxy; 

     return bbox; 
    } 

} 

編輯:初始化地圖本身,縮放級別設置爲3,在這個方法裏面getTileUrl(int x,int y,int zoom)

回答

2

WMSTileProvider.getBoundingBox中,您正在計算以Web墨卡託投影爲單位的邊界框,它們是米。在您的OSGEO_WMS URL字符串中,您指定的bbox單位在EPSG:4326(度)。對於每個圖塊的查詢很可能因此而不正確。

參見WMS reference爲BBOX和SRS:

BBOX:在地圖範圍的邊界框。值是minx,miny,maxx,maxy,單位爲SRS,單位爲

嘗試用EPSG:3857(WebMercator)

免去您的SRS值