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

后端 未结 2 376
一个人的身影
一个人的身影 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:00

    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.

提交回复
热议问题