js:
// 定位
function getLocation(){
if($("#cityName").val().length>1){
return;
}
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
x.innerHTML="定位失败";
y.value="定位失败";
$("#span_city").html("定位失败");
}
}
function showPosition(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//gps定位
$.get("${ctx}/homeAll/getCityByGps", {lat:lat,lng:lng}, function(data){
data = data.trim();
//gps定位失败
if(data==""){
//ip定位
$.get("${ctx}/homeAll/getCityByIp",{},function(res){
res = res.trim();
if(res==""){
//x.innerHTML="定位失败";
//y.value="定位失败";
$("#cityName").val("定位失败");
}else{
//ip定位成功
$("#cityName").val(res);
}
});
}else{
//gps定位成功
$("#cityName").val(data);
}
});
}
$(document).ready(function() {
// 调用定位
getLocation();
});
java:
//根据IP获取城市
@Action(value = "getCityByIp")
public void getCityByIp() {
String city = "";
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
// 以下代码从JSON.java中拷过来的
response.setContentType("text/html");
PrintWriter out = null;
try {
out = response.getWriter();
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
//根据ip返回城市名称
String ipUrl = IP_TAOBAO;
ipUrl=ipUrl.replaceAll("#ip#", ip);
String context = sendGet(ipUrl, null, "utf-8", true);
Gson gson = new Gson();
Json json = gson.fromJson(context, new TypeToken() {}.getType());
if("0".equals(json.getCode())){
city = json.getData().getCity();
city = city.replaceAll("市", "");
}
out.println(city);
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
out.println("");
} finally{
out.flush();
out.close();
}
}
//根据经纬度获取城市
@Action(value = "getCityByGps")
public void getCityByGps() {
String city = "";
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");
// 以下代码从JSON.java中拷过来的
response.setContentType("text/html");
PrintWriter out = null;
try {
out = response.getWriter();
String url = API_MAP_BAIDU;
String param = lat.substring(0,lat.indexOf(".")+6) + "," + lng.substring(0,lng.indexOf(".")+6);
url=url.replaceAll("#location#", param);
String xml = sendGet(url, null, "utf-8", true);
logger.info(xml);
JaxbUtil cityBinder = new JaxbUtil(GeocoderSearchResponse.class,
CollectionWrapper.class);
GeocoderSearchResponse geo = cityBinder.fromXml(xml);
if("0".equals(geo.getStatus())){
city = geo.getResult().get(0).getAddressComponent().get(0).getCity();
city = city.replaceAll("市", "");
}
// 将要被返回到客户端的对象
out.println(city);
} catch (Exception e) {
e.printStackTrace();
logger.error(e);
out.println("");
} finally{
out.flush();
out.close();
}
}