如何在android google map v2实现

时间:2015-05-28 10:02:12

标签: android google-maps-android-api-2 geoserver

我使用geoserve实现来检索我的地图图块。 我请求切片的方式是在地图上的相机更改视图时触发我的api调用,然后发送地图中心视图的lat和lng,然后显示地理服务器层的列表需要TileOverlay的getTileUrl。

但不幸的是,当我的Api返回图层列表时,getTileUrl已经被触发并且我的图块没有渲染。

简而言之,我的geoserver请求的layers参数是动态变化的..所以我的问题是如何触发再次加载地图叠加层,这样当我的Api返回列表时,它会重新加载具有适当层的叠加层。

这是我对OnCameraChangeListener的实现:

private GoogleMap.OnCameraChangeListener cameraChange = new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
        LatLng latLng = map.getCameraPosition().target;
        getLayerLists(latLng);
    }
};

这是getLayerList方法:

private void getLayerList(LatLng latLng) {
    nigsApi.getLayerForArea(TAG_FLOOD_MAP_REQUEST,
            String.valueOf(latLng.latitude), String.valueOf(latLng.longitude),
            onApiResponse);
}

这是返回图层列表的onApiResponse回调:

private OnApiResponse<NigsApi.ResponsePair<List<String>>> onApiResponse = new OnApiResponse<NigsApi.ResponsePair<List<String>>>() {
    @Override
    public void onRequestSuccessful(NigsApi.ResponsePair<List<String>> result) {
        tileProviderFactory.setLayerArea(result.getResult());
    }

    @Override
    public void onRequestFailed(String errorMessage, Object tag) {
        Log.e("Maps", errorMessage);
    }

    @Override
    public void onRequestError(RetrofitError error, Object tag) {
        Log.e("Maps", error.getMessage());
    }
};

在响应之后,它转到My TileProviderFactory类并设置getTileUrl所需的层列表。

以下是getTileUrl的块:

public void setLayerArea(List<String> listLayer){
    this.listLayer = listLayer;
}    

public WmsTileProvider getOsgeoWmsTileProvider() {

    //This is configured for:
    // http://beta.sedac.ciesin.columbia.edu/maps/services
    // (TODO check that this WMS service still exists at the time you try to run this demo,
    // if it doesn't, find another one that supports EPSG:900913
    final String WMS_FORMAT_STRING =
            "http://202.90.153.87:8080/geoserver/flood_100/wms?" +
                    "service=WMS" +
                    "&version=1.1.0" +
                    "&request=GetMap" +
                    "&bbox=%f,%f,%f,%f" +
                    "&width=256" +
                    "&height=256" +
                    "&srs=EPSG:900913" +
                    "&format=image/png" +
                    "&transparent=true" +
                    "&tiled=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 layerTitles = getLayerTitles();

            String s = String.format(Locale.US, WMS_FORMAT_STRING, bbox[MINX],
                    bbox[MINY], bbox[MAXX], bbox[MAXY]);

            s = s + layerTitles;

            Log.e("MapLayers", s);
            URL url = null;

            try {
                url = new URL(s);
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
            return url;
        }
    };

    return tileProvider;
}

方法getLayerTitles()将所有图层连接成一个字符串并将其附加到WMS_FORMAT_STRING网址。

private String getLayerTitles(){
    if (listLayer.size() == 0) {
        return layers;
    } else {
        layers = "&layers=";
        for (String location : listLayer) {
            if (layers.equals("&layers="))
                layers = layers + location;
            else
                layers = layers + "," + location;
        }
        return layers;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要渲染多少个图层?你需要一次渲染所有这些吗?

如果可以,您最好每层创建一个WmsTileProvider;这将使您能够精细地启用或禁用图层。

如果你需要(或想要)一次渲染所有这些,你可以更智能地使用你的getTileUrl:

在WmsTileProvider中创建一个方法,使您可以设置图层(您可以在类中创建mLayers String变量并更改getLayersTitles方法以更改此变量而不是返回它),然后在需要时调用此方法更改图层列表和调用clearTileCache:

https://developers.google.com/android/reference/com/google/android/gms/maps/model/TileOverlay#clearTileCache%28%29

您的图块将使用新图层重新加载...

代码(抱歉格式错误):

public void setLayerArea(List<String> listLayer){
  this.mLayers = getLayerTitles(listLayer);
}   

private String getLayerTitles(List<String> listLayer){
if (listLayer.size() == 0) {
    return "DEFAULT LAYERS";
} else {
    layers = "&layers=";
    for (String location : listLayer) {
        if (layers.equals("&layers="))
            layers = layers + location;
        else
            layers = layers + "," + location;
    }
    return layers;
}
} 

public WmsTileProvider getOsgeoWmsTileProvider() {

//This is configured for:
// http://beta.sedac.ciesin.columbia.edu/maps/services
// (TODO check that this WMS service still exists at the time you try to run this demo,
// if it doesn't, find another one that supports EPSG:900913
final String WMS_FORMAT_STRING =
        "http://202.90.153.87:8080/geoserver/flood_100/wms?" +
                "service=WMS" +
                "&version=1.1.0" +
                "&request=GetMap" +
                "&bbox=%f,%f,%f,%f" +
                "&width=256" +
                "&height=256" +
                "&srs=EPSG:900913" +
                "&format=image/png" +
                "&transparent=true" +
                "&tiled=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, WMS_FORMAT_STRING, bbox[MINX],
                bbox[MINY], bbox[MAXX], bbox[MAXY]);

        s = s + this.mLayers;

        Log.e("MapLayers", s);
        URL url = null;

        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
            throw new AssertionError(e);
        }
        return url;
    }
};

return tileProvider;
}