java 城市定位_手机网页定位当前城市

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值