GIS的学习(二十四)android异步调用geoserver wms中的地图
在geoserver的客户端通过wms的GetMap实现地图的图片的调用和下载。
具体的API如下:
GetMap¶
The GetMap operation requests that the server generate a map. The core parameters specify one or more layers and styles to appear on the map, a bounding box for the map extent, a target spatial reference system, and a width, height, and format for the output, The response is a map image, or other map output artifact, depending on the format requested. GeoServer provides a wide variety of output formats, described in WMS output formats.
The information needed to specify values for parameters such as layers, styles and srs are supplied by the Capabilities document.
A good way to get to know the GetMap parameters is to experiment with the WMS Reflector.
The standard parameters for the GetMap operation are:
Parameter Required? Description
service Yes Service name. Value is WMS.
version Yes Service version. Value is one of 1.0.0, 1.1.0, 1.1.1, 1.3.
request Yes Operation name. Value is GetMap.
layers Yes Layers to display on map. Value is a comma-separated list of layer names.
styles Yes Styles in which layers are to be rendered. Value is a comma-separated list of style names, or empty if default styling is required. Style names may be empty in the list.
srs or crs Yes Spatial Reference System for map output. Value is in form EPSG:nnn. crs is the parameter key used in WMS 1.3.0.
bbox Yes Bounding box for map extent. Value is minx,miny,maxx,maxy in units of the SRS.
width Yes Width of map output, in pixels.
height Yes Height of map output, in pixels.
format Yes Format for the map output. See WMS output formats for supported values.
transparent No Whether the map background should be transparent. Values are true or false. Default is false
bgcolor No Background color for the map image. Value is in the form RRGGBB. Default is FFFFFF (white).
exceptions No Format in which to report exceptions. Default value is application/vnd.ogc.se_xml. Other valid values are application/vnd.ogc.inimage and application/vnd.ogc.se_blank.
GeoServer provides a number of useful vendor-specific parameters, which are documented in the WMS vendor parameters section.
An example request for a PNG map image using default styling is:
http://localhost:8080/geoserver/wms?
request=GetMap
&service=WMS
&version=1.1.1
&layers=topp%3Astates
&styles=
&srs=EPSG%3A4326
&bbox=-145.15104058007,21.731919794922,-57.154894212888,58.961058642578&
&width=780
&height=330
&format=image%2Fpng
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btnFirst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="异步下载方式一"
>
</Button>
<Button
android:id="@+id/btnSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="异步下载方式二"
>
</Button>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:padding="2dp"
>
</ImageView>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</ProgressBar>
</FrameLayout>
</LinearLayout>
主要配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.easyway.geoserver.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".GeoServerGetMapActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
具体代码实现如下:
package com.geoserver.map;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
/**
* 异步下载图片的两种方式
*
*
* 本实例的目的通过geoserver 的wms调用相关的地图图片的服务
* 采用的操作为GetMap实现。相关的操作。
*
*
*
*
* @Title:
* @Description: 实现TODO
* @Copyright:Copyright (c) 2011
* @Company:
* @Date:2012-9-10
* @author longgangbai
* @version 1.0
*/
public class GeoServerGetMapActivity extends Activity implements OnClickListener{
private Button btnFirst;
private Button btnSecond;
private ProgressBar progress;
private FrameLayout frameLayout;
private Bitmap bitmap=null;
ProgressDialog dialog=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFirst=(Button)this.findViewById(R.id.btnFirst);
progress=(ProgressBar)this.findViewById(R.id.progress);
progress.setVisibility(View.GONE);
frameLayout=(FrameLayout)this.findViewById(R.id.frameLayout);
btnFirst.setOnClickListener(this);
btnSecond=(Button)this.findViewById(R.id.btnSecond);
btnSecond.setOnClickListener(this);
}
//前台ui线程在显示ProgressDialog,
//后台线程在下载数据,数据下载完毕,关闭进度框
@Override
public void onClick(View view) {
final String urlString=getGeoServerGetMap();
switch(view.getId()){
case R.id.btnFirst:
dialog = ProgressDialog.show(this, "",
"下载数据,请稍等 …", true, true);
//启动一个后台线程
handler.post(new Runnable(){
@Override
public void run() {
//这里下载数据
try{
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream inputStream=conn.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
break;
case R.id.btnSecond:
MyASyncTask asyncTask=new MyASyncTask();
asyncTask.execute(urlString);
break;
}
}
/**这里重写handleMessage方法,接受到子线程数据后更新UI**/
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg){
switch(msg.what){
case 1:
//关闭
ImageView view=(ImageView)frameLayout.findViewById(R.id.image);
view.setImageBitmap(bitmap);
dialog.dismiss();
break;
}
}
};
public String getGeoServerGetMap(){
StringBuffer sb=new StringBuffer();
sb.append("http://10.100.108.20:8080/geoserver/wms?");//wms路径
sb.append("request=GetMap"); //geoserver的操作
sb.append("&service=WMS"); //服务的类型
sb.append("&version=1.1.1"); //版本
sb.append("&layers=topp:states"); //图层的名称
sb.append("&srs=EPSG:4326"); //图源的类型
sb.append("&bbox=-145.15104058007,21.731919794922,-57.154894212888,58.961058642578"); //图层的范围
sb.append("&width=780");
sb.append("&height=330");
sb.append("&format=image/png");//图片的格式
return sb.toString();
}
class MyASyncTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap=null;
try {
URL url = new URL(params[0]);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
InputStream inputStream=con.getInputStream();
bitmap=BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
//执行获得图片数据后,更新UI:显示图片,隐藏进度条
@Override
protected void onPostExecute(Bitmap Result){
ImageView imgView=(ImageView)frameLayout.getChildAt(0);
imgView.setImageBitmap(Result);
ProgressBar bar=(ProgressBar)frameLayout.getChildAt(1);
bar.setVisibility(View.GONE);
}
}
}
转载自:https://blog.csdn.net/longgangbai/article/details/84287110