本实例在前一博客的基础上,主要是针对四川地理信息中心的地名搜索服务,如果利用ArcGIS for Android API进行解析,并接入地理编码信息进行搜索定位。
1)基础准备:下载Java版的Json库(http://code.google.com/p/json-simple/)、熟悉四川地理信息中心地名搜索规则
2)关于地理地理信息中心地名搜索服务规则,说见其网站,这里不多介绍(http://www.scgis.net/serviceshow.aspx?serviceID=cf917508-8033-4d74-9762-c8c07b7083a0)
3)代码说明:
上一博客提取的代码说明这里就不再介绍了,主要介绍跟地名搜索服务相关的几个文件。
AddressT.java:地名的参数信息
package com.esri.arcgis.android.samples;
public class AddressT {
private int _ID;
private double _X;
private double _Y;
private String _Name;
private String _Type;
private String _Region;
private String _County;
private String _PhoneNumber;
private String _AddressName;
public void set_ID(int _ID) {
this._ID = _ID;
}
public int get_ID() {
return _ID;
}
public void set_X(double _X) {
this._X = _X;
}
public double get_X() {
return _X;
}
public void set_Y(double _Y) {
this._Y = _Y;
}
public double get_Y() {
return _Y;
}
public void set_Name(String _Name) {
this._Name = _Name;
}
public String get_Name() {
return _Name;
}
public void set_Type(String _Type) {
this._Type = _Type;
}
public String get_Type() {
return _Type;
}
public void set_Region(String _Region) {
this._Region = _Region;
}
public String get_Region() {
return _Region;
}
public void set_County(String _County) {
this._County = _County;
}
public String get_County() {
return _County;
}
public void set_PhoneNumber(String _PhoneNumber) {
this._PhoneNumber = _PhoneNumber;
}
public String get_PhoneNumber() {
return _PhoneNumber;
}
public void set_AddressName(String _AddressName) {
this._AddressName = _AddressName;
}
public String get_AddressName() {
return _AddressName;
}
}
EscapeUnescape.java字符串编码及解码文件
package com.esri.arcgis.android.samples;
public class EscapeUnescape {
public static String escape(String src) {
int i;
char j;
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length() * 6);
for (i = 0; i < src.length(); i++) {
j = src.charAt(i);
if (Character.isDigit(j) || Character.isLowerCase(j)
|| Character.isUpperCase(j))
tmp.append(j);
else if (j < 256) {
tmp.append("%");
if (j < 16)
tmp.append("0");
tmp.append(Integer.toString(j, 16));
} else {
tmp.append("%u");
tmp.append(Integer.toString(j, 16));
}
}
return tmp.toString();
}
public static String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(
src.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(
src.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}
}
FindAddress.java:地名搜索类
package com.esri.arcgis.android.samples;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
//import org.json.simple.JSONArray;
//import org.json.simple.JSONObject;
//import org.json.simple.parser.JSONParser;
import org.json.JSONArray;
import org.json.JSONObject;
public class FindAddress {
//&pretty=true
private String BaseLocation="http://www.scgis.net.cn/imap/iMapServer/NewRest/services/SCNameSearch/Search?StartIndex=0&StopIndex=5&pretty=true";
private String tokenURL="http://www.scgis.net.cn/imap/iMapServer/Token/getToken?username=test20110703&password=test20110703&IpAddress=&TimeSpan=100M";
private String keyname="";
private String token="";
private String city="";
private String county="";
public FindAddress(String keyname)
{
this.keyname=EscapeUnescape.escape(keyname);
this.token=EscapeUnescape.escape(new GetToken().sendPost(tokenURL));
}
public FindAddress(String keyname,String city)
{
this.keyname=EscapeUnescape.escape(keyname);
this.city=EscapeUnescape.escape(city);
this.token=new GetToken().sendPost(tokenURL);
}
public FindAddress(String keyname,String city,String county)
{
this.keyname=EscapeUnescape.escape(keyname);
this.city=EscapeUnescape.escape(city);
this.county=EscapeUnescape.escape(county);
this.token=new GetToken().sendPost(tokenURL);
}
public ArrayList<AddressT> GetResult()
{
ArrayList<AddressT> result=new ArrayList<AddressT>();
String url=this.BaseLocation+
//"&DiQuKey"+this.city+
//"&QuXianKey"+this.county+
"&keyname="+this.keyname+
"&token="+this.token;
//URLEncoder.encode();
StringBuilder lineJson=new StringBuilder ();
try {
URL httpurl = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) httpurl
.openConnection();
httpConn.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
httpConn.getInputStream(),"UTF-8"));
String line;
AddressT oneAddress;
while ((line = in.readLine()) != null) {
//System.out.println(line);
lineJson.append(line);
}
in.close();
//test
/*oneAddress=new AddressT();
oneAddress.set_ID(1);
oneAddress.set_X(31.1);
oneAddress.set_Y(101.2);
oneAddress.set_Name(lineJson.toString());
result.add(oneAddress);*/
//System.out.println(lineJson);
//decode
//JSONParser parser = new JSONParser();
//Object obj = parser.parse(lineJson.toString());
//JSONObject allPackage = (JSONObject) obj;
JSONObject allPackage = new JSONObject(lineJson.toString());
//get false return
if(!(Boolean) allPackage.get("success"))
{
return null;
}
//frome message to features...decode
//Object objFeatures=((JSONObject)allPackage.get("message")).get("features");
// to array
//JSONArray arrayFeatures = (JSONArray) objFeatures;
JSONArray arrayFeatures=allPackage.getJSONObject("message").getJSONArray("features");
//int count=arrayFeatures.size();
for(int i=0;i<arrayFeatures.length();i++)
{
//JSONArray oneFeature = (JSONArray) ((JSONObject) objT).get("attributes");
JSONArray oneFeature=arrayFeatures.getJSONObject(i).getJSONArray("attributes");
oneAddress=new AddressT();
//Integer.parseInt(oneFeature.get(0).toString());
oneAddress.set_ID(Integer.parseInt(oneFeature.get(0).toString()));
oneAddress.set_X(Double.parseDouble(oneFeature.get(1).toString()));
oneAddress.set_Y(Double.parseDouble(oneFeature.get(2).toString()));
oneAddress.set_Name(oneFeature.get(3).toString());
oneAddress.set_Type(oneFeature.get(4).toString());
oneAddress.set_Region(oneFeature.get(5).toString());
oneAddress.set_County(oneFeature.get(6).toString());
//oneAddress.set_PhoneNumber(oneFeature.get(7).toString());
//oneAddress.set_AddressName(oneFeature.get(8).toString());
//add it
result.add(oneAddress);
}
} catch (Exception e) {
//test
AddressT oneAddress=new AddressT();
oneAddress.set_ID(3);
oneAddress.set_X(31.1);
oneAddress.set_Y(101.2);
oneAddress.set_Name(e.getMessage());
result.add(oneAddress);
e.printStackTrace();
}
return result;
}
}
效果如图:
由于系列代码比较多,需要学习此系列编码的同学,可以留下邮箱,空了我给你们发过去!