是的,这很有可能,但这又取决于客户端的网络浏览器是否supports http://caniuse.com/geolocation HTML5 navigator.geolocation https://developer.mozilla.org/en-US/docs/WebAPI/Using_geolocation和/或您正在使用谷歌加载器API https://developers.google.com/loader/作为后备。
您可以通过以下方式获取对具体 GMap JavaScript 对象的引用widgetVarName.getMap()
where widgetVarName
is the <p:gmap widgetVar>
。然后您可以使用GMap JavaScript API https://developers.google.com/maps/documentation/javascript/reference通常的方法,例如setCenter()
.
这是一个启动示例,假设您想通过 HTML5 检查地理位置navigator.geolocation
并使用 Google Loader API 作为后备。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<p:gmap widgetVar="w_gmap" type="HYBRID" center="41.381542, 2.122893" zoom="15" style="width:600px;height:400px" />
<script type="text/javascript">
if (navigator.geolocation) {
checkGeolocationByHTML5();
} else {
checkGeolocationByLoaderAPI(); // HTML5 not supported! Fall back to Loader API.
}
function checkGeolocationByHTML5() {
navigator.geolocation.getCurrentPosition(function(position) {
setMapCenter(position.coords.latitude, position.coords.longitude);
}, function() {
checkGeolocationByLoaderAPI(); // Error! Fall back to Loader API.
});
}
function checkGeolocationByLoaderAPI() {
if (google.loader.ClientLocation) {
setMapCenter(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
} else {
// Unsupported! Show error/warning?
}
}
function setMapCenter(latitude, longitude) {
w_gmap.getMap().setCenter(new google.maps.LatLng(latitude, longitude));
}
</script>
注:初始中心位置41.381542, 2.122893
是复制粘贴自PrimeFaces 展示 http://www.primefaces.org/showcase/ui/gmapBasic.jsf我认为它代表了他们最喜欢的俱乐部的足球场:)你可以随意将其更改为其他任何东西,例如您自己公司的位置。
另请注意,出于安全原因,普通网络浏览器会要求最终用户确认共享位置。也可以看看谷歌浏览器文档 https://support.google.com/chrome/answer/142065?hl=en and Mozilla 火狐文档 http://www.mozilla.org/en-US/firefox/geolocation/就此主题而言。你cannot关闭这部分。最终用户必须明确接受该请求。如果最终用户不允许,它将停留在初始中心位置。