Google maps 在gmap中居中到当前位置
是否可以使用Primefaces的gmap将谷歌地图集中在客户当前位置 我使用JSF、JPA和primefaces开发了一个公共卫生信息系统。配备GPS移动设备的野战军官需要记录位置,以便将数据记录到数据库中进行分析 我们可以用Primefaces gmap吗Google maps 在gmap中居中到当前位置,google-maps,jsf,primefaces,Google Maps,Jsf,Primefaces,是否可以使用Primefaces的gmap将谷歌地图集中在客户当前位置 我使用JSF、JPA和primefaces开发了一个公共卫生信息系统。配备GPS移动设备的野战军官需要记录位置,以便将数据记录到数据库中进行分析 我们可以用Primefaces gmap吗 如果不可能,我还可以使用哪些其他工具和技术来实现此功能?是的,这是很有可能的,但这反过来也取决于客户端的webbrowser HTML5和/或您是否使用as回退 <script type="text/javascript" src=
如果不可能,我还可以使用哪些其他工具和技术来实现此功能?是的,这是很有可能的,但这反过来也取决于客户端的webbrowser HTML5和/或您是否使用as回退
<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>
您可以通过widgetVarName.getMap()
获取对具体GMap JavaScript对象的引用,其中widgetVarName
是
。然后,您可以使用其上的方法,如setCenter()
这里有一个启动示例,假设您希望通过HTML5navigator.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>
if(导航器.地理位置){
通过HTML5()检查地理位置;
}否则{
checkGeolocationByLoaderAPI();//不支持HTML5!返回到加载程序API。
}
函数checkGeolocationByHTML5(){
navigator.geolocation.getCurrentPosition(函数(位置){
setMapCenter(position.coords.latitude,position.coords.longitude);
},函数(){
checkGeolocationByLoaderAPI();//错误!返回到加载程序API。
});
}
函数checkGeolocationByLoaderAPI(){
if(google.loader.ClientLocation){
setMapCenter(google.loader.ClientLocation.latitude、google.loader.ClientLocation.longitude);
}否则{
//不支持!是否显示错误/警告?
}
}
函数setMapCenter(纬度、经度){
w_gmap.getMap().setCenter(新的google.maps.LatLng(纬度、经度));
}
注意:41.381542,2.122893
的初始中心位置是复制粘贴的,我认为它代表了他们最喜欢的俱乐部的足球场:)您可以随意将其更改为任何其他位置,例如您自己公司的位置
还要注意的是,出于安全原因,一般的网络浏览者会要求最终用户确认共享该位置。另请参见和关于该主题。您不能关闭此部件。最终用户必须明确地接受请求。如果最终用户不允许,它将停留在初始的中心位置。对于现在阅读此内容的任何人来说,它现在应该作为PF('w_gmap').getMap().setCenter(新的google.maps.LatLng(纬度、经度))访问(除非我错了)代码>什么是PF?我发现getMap()未定义有任何线索吗?我正在运行primeface 3.5。你知道我应该包括什么吗?