package com.jqyd.app;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import com.jqyd.model.GPSDataModel;
public class PublicDeal {
private static double EARTH_RADIUS = 6371110;
public double getDistance(GPSDataModel gps1, GPSDataModel gps2) {
double distance = 0.0;
double radLat1 = rad(gps1.getLatitude());
double radLat2 = rad(gps2.getLatitude());
double a = radLat1 - radLat2; // 纬度差
double b = rad(gps1.getLongitude()) - rad(gps2.getLongitude()); // 经度差
double s = 2 * Math.asin((Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2))));
s = s * EARTH_RADIUS;
distance = Math.round(s);
return distance;
}
private double rad(double d) {
return d * Math.PI / 180.0;
}
// 获取文字信息的方法
public String GetText(double dLong, double dLat, String locate_mode
) {
// 使用StringBuffer来拼接XML文件
StringBuffer xml = new StringBuffer();
xml.append("<?xml version=\"1.0\" encoding='GB2312'?>\n");
xml.append("<spatial_request method=\"searchPoint\">\n");
xml.append("<x>" + dLong + "</x>\n");
xml.append("<y>" + dLat + "</y>\n");
xml.append("<poiNumber>1</poiNumber>\n");
xml.append("<range>50</range>\n");
xml.append("<pattern>0</pattern>\n");
xml.append("<roadLevel>1</roadLevel>\n");
xml.append("</spatial_request>");
String xml_ = null;
try {
xml_ = URLEncoder.encode(xml.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 字符串拼接
String temp = new String("");
try {
String responseMessageXml = null;
String strUrl = "http://search1.mapabc.com/sisserver?&config=SPAS&enc=GB2312&spatialXml="
+ xml_
+ "&a_k=b0a7db0b3a30f944a21c3682064dc70ef5b738b062f6479a5eca39725798b1ee300bd8d5de3a4ae3";
// System.out.println(strUrl);
String lonns = String.valueOf(dLong);
String latts = String.valueOf(dLat);
responseMessageXml = GetUrlResult(strUrl);
if (responseMessageXml.indexOf("<Province ver=\"1.0\">") != -1) {
// 准备
// 省
int provinceBegin;
int provinceEnd;
String provinceResult;
provinceBegin = responseMessageXml
.indexOf("<Province ver=\"1.0\">");
provinceEnd = responseMessageXml.indexOf("</Province>",
provinceBegin + 20);
provinceResult = responseMessageXml.substring(
provinceBegin + 19, provinceEnd);
// 市
int cityBegin;
int cityEnd;
String cityResult;
cityBegin = responseMessageXml.indexOf("<City ver=\"1.0\">");
cityEnd = responseMessageXml.indexOf("</City>", cityBegin + 17);
cityResult = responseMessageXml.substring(cityBegin + 17,
cityEnd);
// 区
int districtBegin;
int districtEnd;
String districtResult;
districtBegin = responseMessageXml
.indexOf("<District ver=\"1.0\">");
districtEnd = responseMessageXml.indexOf("</District>",
districtBegin + 20);
districtResult = responseMessageXml.substring(
districtBegin + 20, districtEnd);
// 路
String nResult3 = "";
String nResult4 = "";
String nResult5 = "";
String nResult3_1 = "";
if (responseMessageXml.indexOf("<Road ver=\"1.0\">") != -1) {
int roadBegin1;
int roadEnd1;
String roadResult1;
roadBegin1 = responseMessageXml
.indexOf("<Road ver=\"1.0\">");
roadEnd1 = responseMessageXml.indexOf("</Road>",
roadBegin1 + 16);
roadResult1 = responseMessageXml.substring(roadBegin1 + 16,
roadEnd1);
int roadBegin2;
int roadEnd2;
String roadResult2;
roadBegin2 = responseMessageXml.indexOf(
"<Road ver=\"1.0\">", roadBegin1 + 16);
roadEnd2 = responseMessageXml.indexOf("</Road>",
roadBegin2 + 16);
roadResult2 = responseMessageXml.substring(roadBegin2 + 16,
roadEnd2);
// freeFormAddress
int nBeginIndex3;
int nEndIndex3;
nBeginIndex3 = roadResult1.indexOf("<name>");
nEndIndex3 = roadResult1.indexOf("</name>",
nBeginIndex3 + 6);
nResult3 = new String(roadResult1.substring(
nBeginIndex3 + 6, nEndIndex3));
int nBeginIndex4;
int nEndIndex4;
nBeginIndex4 = roadResult1.indexOf("<direction>");
nEndIndex4 = roadResult1.indexOf("</direction>",
nBeginIndex4 + 11);
nResult4 = new String(roadResult1.substring(
nBeginIndex4 + 11, nEndIndex4));
if ("WestSouth".equals(nResult4)) {
nResult4 = "西南方";
} else if ("EastNorth".equals(nResult4)) {
nResult4 = "东北方";
}
int nBeginIndex5;
int nEndIndex5;
nBeginIndex5 = roadResult1.indexOf("<distance>");
nEndIndex5 = roadResult1.indexOf("</distance>",
nBeginIndex5 + 10);
nResult5 = new String(roadResult1.substring(
nBeginIndex5 + 10, nEndIndex5));
String n5[] = nResult5.split("\\.");
nResult5 = n5[0];
// 辅路
int nBeginIndex3_1;
int nEndIndex3_1;
nBeginIndex3_1 = roadResult2.indexOf("<name>");
nEndIndex3_1 = roadResult2.indexOf("</name>",
nBeginIndex3 + 6);
nResult3_1 = new String(roadResult2.substring(
nBeginIndex3_1 + 6, nEndIndex3_1));
}
// poi
String poiR = "";
String poiR_ = "";
if (responseMessageXml.indexOf("<poi>") != -1) {
int poiBegin;
int poiEnd;
String poiResult;
poiBegin = responseMessageXml.indexOf("<poi>");
poiEnd = responseMessageXml.indexOf("</poi>", poiBegin + 5);
poiResult = responseMessageXml.substring(poiBegin + 5,
poiEnd);
int poiStart;
int poiStop;
poiStart = poiResult.indexOf("<name>");
poiStop = poiResult.indexOf("</name>", poiStart + 6);
poiR = new String(poiResult
.substring(poiStart + 6, poiStop));
// 2
int poiBegin_;
int poiEnd_;
String poiResult_;
poiBegin_ = responseMessageXml.indexOf("<poi>",
poiBegin + 5);
poiEnd_ = responseMessageXml.indexOf("</poi>",
poiBegin_ + 5);
poiResult_ = responseMessageXml.substring(poiBegin_ + 5,
poiEnd_);
int poiStart_;
int poiStop_;
poiStart_ = poiResult_.indexOf("<name>");
poiStop_ = poiResult_.indexOf("</name>", poiStart_ + 6);
poiR_ = new String(poiResult_.substring(poiStart_ + 6,
poiStop_));
}
// 交叉路口
String cross = "";
if (responseMessageXml.indexOf("<crossPoiList type=\"list\">") != -1) {
int crossBegin;
int crossEnd;
String crossResult;
crossBegin = responseMessageXml.indexOf("<cross>");
crossEnd = responseMessageXml.indexOf("</cross>",
crossBegin + 7);
crossResult = responseMessageXml.substring(crossBegin + 7,
crossEnd);
int crossStart;
int crossStop;
crossStart = crossResult.indexOf("<name>");
crossStop = crossResult.indexOf("</name>", crossStart + 6);
cross = new String(crossResult.substring(crossStart + 6,
crossStop));
}
// 解析XML文件
// province
int nBeginIndex0;
int nEndIndex0;
String nResult0 = "";
if (provinceResult.indexOf("<name>") != -1) {
// 返回在此字符串中最右边出现的指定子字符串的索引
nBeginIndex0 = provinceResult.indexOf("<name>");
nEndIndex0 = provinceResult.indexOf("</name>",
nBeginIndex0 + 6);
nResult0 = new String(provinceResult.substring(
nBeginIndex0 + 6, nEndIndex0));
}
// city
int nBeginIndex1;
int nEndIndex1;
String nResult1 = "";
// if(cityResult.indexOf("<name>")!=-1){
nBeginIndex1 = cityResult.indexOf("<name>");
nEndIndex1 = cityResult.indexOf("</name>", nBeginIndex1 + 6);
nResult1 = new String(cityResult.substring(nBeginIndex1 + 6,
nEndIndex1));
if ("".equals(nResult1)) {
nResult1 = " ";
}
// }
// county
int nBeginIndex2;
int nEndIndex2;
String nResult2 = "";
if (districtResult.indexOf("<name>") != -1) {
nBeginIndex2 = districtResult.indexOf("<name>");
nEndIndex2 = districtResult.indexOf("</name>",
nBeginIndex2 + 6);
nResult2 = new String(districtResult.substring(
nBeginIndex2 + 6, nEndIndex2));
if (nResult2.equals("")) {
nResult2 = "市辖区";
}
}
// System.out.println(poiR);
// 具体实现详细内容的拼接,并且输出
if (!nResult0.equals("")) {
temp = nResult0;
}
if (!nResult1.equals("")) {
temp = temp + ":" + nResult1;
}
if (!nResult2.equals("")) {
temp = temp + ":" + nResult2;
}
if (!cross.equals("")) {
String n6[] = cross.split("-");
String q = n6[0];
String h = n6[1];
if (q.equals(h)) {
cross = q;
}
temp = temp + ":" + cross;
if (!poiR.equals("")) {
temp = temp + "," + poiR + ",";
}
if (!poiR_.equals("")) {
temp = temp + poiR_ + "附近";
}
} else {
if (!poiR.equals("")) {
temp = temp + ":" + poiR + ",";
}
if (!poiR_.equals("")) {
temp = temp + poiR_ + "附近";
}
}
temp = temp + ":" + "经度" + lonns + "纬度" + latts + ","
+ locate_mode + "定位,位于";
} else {
String txt[] = responseMessageXml.split("[.]");
temp = txt[0] + ":" + txt[0] + ":" + txt[1] + ":" + "" + ":"
+ "经度" + lonns + "纬度" + latts + "," + locate_mode
+ "定位,位于";
}
} catch (Exception ex) {
ex.printStackTrace();
}
return temp;
}
public Map jp(double dLong, double dLat) {
String xresult = "";
String yresult = "";
Map m = new HashMap();
try {
String responseMessageXml = null;
String strUrl = "http://search1.mapabc.com/sisserver?config=RGC&resType=xml&x1="
+ dLong
+ "&y1="
+ dLat
+ "&cr=0&flag=true&a_k=6e37dfb9fe56979897e25cc1a5b39e2e2dcebfe4d2d30ee3433e405b3572745d3f40c785c83a4833";
responseMessageXml = GetUrlResult(strUrl);
// System.out.println(responseMessageXml);
// x
int xbegin;
int xend;
xbegin = responseMessageXml.indexOf("<x>");
xend = responseMessageXml.indexOf("</x>", xbegin + 3);
xresult = responseMessageXml.substring(xbegin + 3, xend);
// y
int ybegin;
int yend;
ybegin = responseMessageXml.indexOf("<y>");
yend = responseMessageXml.indexOf("</y>", ybegin + 3);
yresult = responseMessageXml.substring(ybegin + 3, yend);
m.put("x", xresult);
m.put("y", yresult);
} catch (Exception ex) {
ex.printStackTrace();
}
return m;
}
// 传入url,传出该http请求的结果(文字描述请求和短信请求,计费请求)
public String GetUrlResult(String url) {
String result = "";
try {
URL serviceurl = new URL(url);
URLConnection connection = serviceurl.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
} catch (Exception e) {
System.out.println("请求URL服务时发生错误:" + e);
}
return result;
}
}