Posted on 2010-09-08 14:20
TWaver 阅读(2112)
评论(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