如何从后端获取 java 网络应用程序中的用户位置?
How to fetch user location in java web application from backend?
问题
如何从后端获取 java 网络应用程序中的用户位置?
解决方案
我尝试了 Geolocation api 从其余部分获取纬度和纬度 api 但是当我将它部署在服务器上时测试用例失败了,因为它正在获取服务器位置的纬度和经度。
而且我很困惑如何向 url 添加其他参数以使其完美运行。
下面是我的代码 -
public static JSONObject getLatLon() {
JSONObject Jobject = null;
String json = createJson();
LOG.info("Json is " + json);
okhttp3.RequestBody body = okhttp3.RequestBody.create(JSON, json);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.googleapis.com/geolocation/v1/geolocate?key=XXX")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
LOG.info("response is " + response);
String resStr = response.body().string();
LOG.info("response String is " + resStr);
Jobject = new JSONObject(resStr);
LOG.info("Json Object is " + Jobject);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return Jobject;
}
public static String createJson() {
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
return mapper.writeValueAsString(json);
} catch (JsonProcessingException e) {
}
return null;
}
Can anyone guide me how can I fetch latitute and longitute so that endUser location can be fetched?
请问您想访问谁的坐标?如果您想访问最终用户/访问者 IP 地址,Google 地理定位 API 将不起作用。
HTML5 地理定位是一项客户端功能,需要客户批准才能接收他们的坐标。
您可以尝试的最佳选择是使用访问者 IP 地址并使用地理定位数据库进行估计。您可以从带有坐标的免费 IP2Location LITE DB5 开始。
问题
如何从后端获取 java 网络应用程序中的用户位置?
解决方案
我尝试了 Geolocation api 从其余部分获取纬度和纬度 api 但是当我将它部署在服务器上时测试用例失败了,因为它正在获取服务器位置的纬度和经度。
而且我很困惑如何向 url 添加其他参数以使其完美运行。
下面是我的代码 -
public static JSONObject getLatLon() {
JSONObject Jobject = null;
String json = createJson();
LOG.info("Json is " + json);
okhttp3.RequestBody body = okhttp3.RequestBody.create(JSON, json);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.googleapis.com/geolocation/v1/geolocate?key=XXX")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
LOG.info("response is " + response);
String resStr = response.body().string();
LOG.info("response String is " + resStr);
Jobject = new JSONObject(resStr);
LOG.info("Json Object is " + Jobject);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return Jobject;
}
public static String createJson() {
ObjectMapper mapper = new ObjectMapper();
String json = null;
try {
return mapper.writeValueAsString(json);
} catch (JsonProcessingException e) {
}
return null;
}
Can anyone guide me how can I fetch latitute and longitute so that endUser location can be fetched?
请问您想访问谁的坐标?如果您想访问最终用户/访问者 IP 地址,Google 地理定位 API 将不起作用。
HTML5 地理定位是一项客户端功能,需要客户批准才能接收他们的坐标。
您可以尝试的最佳选择是使用访问者 IP 地址并使用地理定位数据库进行估计。您可以从带有坐标的免费 IP2Location LITE DB5 开始。