1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* 獲取指定IP對應的經緯度(為空返回當前機器經緯度)
*
* @param ip
* @return
*/
public
static
String[] getIPXY(String ip) {
String ak =
"百度申請的Key"
;
if
(
null
== ip) {
ip =
""
;
}
try
{
URL url =
new
URL(
"http://api.map.baidu.com/location/ip?ak="
+ ak
+
"&ip="
+ ip +
"&coor=bd09ll"
);
InputStream inputStream = url.openStream();
InputStreamReader inputReader =
new
InputStreamReader(inputStream);
BufferedReader reader =
new
BufferedReader(inputReader);
StringBuffer sb =
new
StringBuffer();
String str;
do
{
str = reader.readLine();
sb.append(str);
}
while
(
null
!= str);
str = sb.toString();
if
(
null
== str || str.isEmpty()) {
return
null
;
}
int
index = str.indexOf(
"point"
);
int
end = str.indexOf(
"}}"
, index);
if
(index == -
1
|| end == -
1
) {
return
null
;
}
str = str.substring(index -
1
, end +
1
);
if
(
null
== str || str.isEmpty()) {
return
null
;
}
String[] ss = str.split(
":"
);
if
(ss.length !=
4
) {
return
null
;
}
String x = ss[
2
].split(
","
)[
0
];
String y = ss[
3
];
x = x.substring(x.indexOf(
"\""
) +
1
, x.indexOf(
"\""
,
1
));
y = x.substring(y.indexOf(
"\""
) +
1
, y.indexOf(
"\""
,
1
));
return
new
String[] { x, y };
}
catch
(MalformedURLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
null
;
}