4

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?

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92

2 Answers2

4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC, can you also suggest any stable IP GEO LOCATION., coz there are lot of'em out there, I'm kinda having hard time to figure which one to use. Please provide any suggestions. – Kishor Prakash Mar 20 '13 at 12:34
  • @BalusC there is an issue with private and reserved ips, many tools fail to geolocate with that type of ip, is there a workaround for those? – BugsForBreakfast Jul 04 '19 at 15:04
  • @KishorPrakash did you came into the issue I mention above mate? – BugsForBreakfast Jul 04 '19 at 15:19
3

In addition to BalusC's answer a quote from the Google Maps API documentation (Primefaces uses this API under the hood):

Newer browsers are starting to support the W3C Geolocation standard. This standard is part of HTML5 and will likely become the de-facto standard going forward. All applications that wish to perform geolocation should support this standard.

Some browsers use IP addresses to detect a user's location; however, As a user's IP address can only provide a rough estimate of a user's location, we don't recommend using this approach for geolocation. The W3C approach is the easiest and most fully-supported so it should be prioritized over other methods.

Here is a short example from the given link:

var initialLocation;
var browserSupportFlag =  new Boolean();
// Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  }

Normally, the user will be prompted and asked for permission when a website tries to access location information.

You could first try this and if it fails go on with Balusc's answer.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • I'm aware of this method. What if the user rejects to provide his Location information? I mean some user will just cancel the prompt without looking at it,thinking its some update information or something. – Kishor Prakash Mar 20 '13 at 12:31
  • The complete source is shown in the given link. They first try to get the location and if that fails they take "Siberia" ;-). You then should try to find the location with the IP address as proposed by BalusC. – Matt Handy Mar 20 '13 at 12:38
  • @MattHandy Hey man I implemented the geolocation via ip like BalusC suggests but I came into an issue that is most devices have a private or reserved ip and so most ip location service fails when they receive that kind of ip, do you know a workaround for that or a service that works with that kind of ip? I appreciate any help :) – BugsForBreakfast Jul 04 '19 at 15:15