如何在 Primefaces Gmap 上标记用户的当前位置? [英] how to mark user's current location on Primefaces Gmap?

查看:20
本文介绍了如何在 Primefaces Gmap 上标记用户的当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Gmap 中,我们可以通过传递自定义纬度和经度来标记所需的位置.有什么方法可以为用户的当前位置添加标记到地图上.?有没有办法获取用户的经纬度?有没有人做过?请分享您的想法?

In Gmap we can mark, desired location by passing custom latitude and longitude. Is there any way to add marker to map for user's CURRENT LOCATION.? Is there a way to get user's latitude and longitude? Has any one done it before? Please share your idea?

推荐答案

您需要计算出用户的 IP 地址,然后将其提供给某些 IP 地理定位工具/API,以获取纬度/经度的地理位置.最后将此信息用于您的 Google 地图.

You need to figure the user's IP address and then feed it to some IP geo location tool/API in order to get the geo location in latitude/longitude. Finally use this information for your Google Map.

JSF 能为您做的就是为您提供用户的 IP 地址,如下所示:

All JSF can do for you is giving you the user's IP address as follows:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
String ip = request.getRemoteAddr();
// ...

如果您还想考虑代理,请检查 X-Forwarded-For 标头:

If you'd like to take proxies into account as well, then check X-Forwarded-For header:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
String ip = request.getRemoteAddr();
String forwardedFor = request.getHeader("X-Forwarded-For");

if (forwardedFor != null) {
    ip = forwardedFor.split("\s*,\s*", 2)[0];
}

// ...

您又必须使用哪个 IP 地理定位工具/API 是一个您必须自己回答的问题.只需向 Google 输入关键字ip geo location"即可开始使用.

Which IP geo location tool/API you in turn have to use is a question which you've to answer yourself. Just feed the keywords "ip geo location" to Google to get started.

这篇关于如何在 Primefaces Gmap 上标记用户的当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆