Posted on 2010-09-08 14:20
TWaver 閱讀(2121)
評論(5) 編輯 收藏
最近老是有莫名的客戶訪問公司網站,一直假扮神神秘秘,不肯透露自己的公司信息,搞得公司負責營銷的mm很是郁悶,無法登記客戶的來源信息,她一直抱怨說,哪怕留個大概的地址也好交差啊。看在mm平時也挺幫忙的面子上,我決定幫她解決一下這種煩惱。如果能把對方的位置給標在地圖上,讓mm把圖片存下來,整理資料的時候一起提交,這不就就可以解決一下她的煩惱了嘛,小意思,馬上行動。
他/她/它訪問網站的時候,除去留言,再也無從找起其它文字信息了,依靠對方主動提供給我們需要的信息,還是不太可能的,那我們就主動獲取,訪問ip是他留下的最大的可用信息點,那我們就從他/她/它的訪問ip入手,有了ip就可以得知他/她/它的位置信息,經緯度是往往最經常見的一種,然后再使用公司的GIS組件,把這經緯度釘在地圖上,那就一切ok了。
首先從mm手里要來對方的登錄ip,然后又翻出上次公司老外過來交流時給我的一個免費的ip地址查詢服務地址,馬上coding!!
先寫一個查詢ip信息的類
1
public class LocateIP
{
2
private final static String ApiUrl = "http://ipinfodb.com/ip_query.php?ip=";
3
public static GeoSite GetUserLocation(String ipAddress) throws IOException
{
4
if (ipAddress == null)
{
5
return null;
6
}
7
GeoSite site = null;
8
String reqUrl = ApiUrl + ipAddress;
9
URL url = new URL(reqUrl);
10
String content = XMLUtils.getRequestResult(url);
11
System.out.println(content);
12
Document doc = XMLUtils.createDocument(content,"UTF-8");
13
if (doc != null)
{
14
Node root = XMLUtils.getChild(doc, "Response");
15
site = new GeoSite();
16
site.setIP(XMLUtils.getSonValue(root, "Ip"));
17
site.setCountry(XMLUtils.getSonValue(root, "CountryName"));
18
site.setCity(XMLUtils.getSonValue(root, "City"));
19
site.setZipCode(XMLUtils.getSonValue(root, "ZipPostalCode"));
20
String longitude = XMLUtils.getSonValue(root, "Longitude");
21
String latitude = XMLUtils.getSonValue(root, "Latitude");
22
if ((longitude != null) && (!longitude.trim().equals(""))
23
&& (latitude != null)
24
&& (!latitude.trim().equals("")))
{
25
double lng = Double.parseDouble(longitude);
26
double lat = Double.parseDouble(latitude);
27
site.setCoordinate(new GeoCoordinate(lng,lat ));
28
}
29
}
30
return site;
31
}
32
}
然后再創建一個簡單的界面
1
public class LocateWhereRU extends JApplet
{
2
public void init()
{
3
final TNetwork network = new TNetwork();
4
final GisNetworkAdapter adapter = new GisNetworkAdapter(network);
5
adapter.installAdapter();
6
(adapter.getMap()).addLayer(TWaverGisConst.TILEMAP_LAYERNAME_GOOGLESTATELLITELAYER,
7
TWaverGisConst.EXECUTOR_TYPE_GOOGLESTATELATEMAP);
8
new Thread(new Runnable()
{
9
public void run()
{
10
GeoSite site = null;
11
try
{
12
site = LocateIP.GetUserLocation("58.33.101.102");
13
} catch (IOException e)
{
14
System.out.println("catch exception " + e.getMessage());
15
e.printStackTrace();
16
}
17
if (site != null)
{
18
final Rack rack = new Rack();
19
final GeoCoordinate center = site.getCoordinate();
20
rack.putClientProperty(TWaverGisConst.GEOCOORDINATE, center);
21
rack.putCustomDraw(true);
22
rack.putCustomDrawFill(true);
23
rack.putCustomDrawShapeFactory(TWaverConst.SHAPE_CIRCLE);
24
rack.addAttachment(TWaverConst.ATTACHMENT_MESSAGE);
25
rack.putMessageContent("神秘人在這里");
26
rack.setSize(5, 5);
27
try
{
28
sendMail(site.getCity() + "," + site.getCountry());
29
} catch (Exception e)
{
30
e.printStackTrace();
31
}
32
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
33
public void run()
{
34
if (center != null)
{
35
GeographyMap tm = adapter.getMap();
36
tm.setCenterPoint(center);
37
tm.setZoom(12);
38
}
39
network.clearMovableFilters();
40
network.getDataBox().addElement(rack);
41
}
42
});
43
}
44
}
45
}).start();
46
Container container = getContentPane();
47
container.setLayout(new BorderLayout());
48
container.add(network, BorderLayout.CENTER);
49
container.add(new StatusBar(adapter.getMap(), network.getCanvas()),
50
BorderLayout.SOUTH);
51
}
52
}
ok,抓緊運行,原來這是一位藏在上海的他/她/它

當然,如果想看更詳細,可以打開衛星圖,把地圖推到足夠精度上,這下mm可以交差了。
源代碼、第三方lib包、可執行包、run.bat都在附件中,請大家自行下載。
ipdemo.zip