本人想开发一个网站,希望可以通过google map api实现:
获取访问者的IP,然后定位到google map的相应区域,如广州的用户访问,则定位到广州的地图
请问要怎么实现,希望能给出可以发布到tomcat下相应的例子,谢谢!
[url]http://www.codechina.org/doc/google/gmapapi/[/url]
[code="javascript"]
var map = new GMap2(document.getElementById("map"));
var geocoder = new GClientGeocoder();
function showAddress(address) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address);
}
}
);
}
[/code]
java 获得访问者IP
package com.util;
import javax.servlet.http.HttpServletRequest;
public class GetIPAddr {
public String getIP(HttpServletRequest request) {
String ip = request.getHeader(" x-forwarded-for ");
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" Proxy-Client-IP ");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getHeader(" WL-Proxy-Client-IP ");
}
if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
String[] arg=ip.split(",");
return arg[0];
}
}