how to mark user's current location on Primefaces Gmap?

后端 未结 2 373
一个人的身影
一个人的身影 2021-01-13 17:10

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 us

2条回答
  •  走了就别回头了
    2021-01-13 18:02

    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.

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

    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];
    }
    
    // ...
    

    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.

提交回复
热议问题