如何使用ZK框架动态获取当前位置?

How to get the current location dynamically using ZK framework?

我是 java 的 zk 框架的新手,我希望能够动态地创建地图和检索当前位置,目前这是我最新的地图代码(lat 和 lng 是硬编码的这就是问题):

        <div apply="br.com.minifrete.maps.GoogleMapsController">
        <!-- define the property 'zk.googleAPIkey' (or any other name) 
            as a ZK-library-property, VM-arg or system-property 
            or simply add your APIkey inline instead of ${c:property...} -->
        <!--<script type="text/javascript" content="zk.googleAPIkey='AIzaSyANBdLQzy9acJxEWCc7UOHhpIU9Zv94ZJ0'" />-->
        <script type="text/javascript"
            content="zk.googleAPIkey='AIzaSyANBdLQzy9acJxEWCc7UOHhpIU9Zv94ZJ0'" />
        <gmaps version="3.26" id="gmaps" width="520px"
            height="400px" showSmallCtrl="true" protocol="https">
            <ginfo id="info" open="true"
                content="Hello, &lt;a href=&quot;http://www.zkoss.org\&quot;&gt;ZK&lt;/a&gt;." />
            <gmarker id="marker" lat="37.4410" lng="-122.1490"
                content="Hello, &lt;a href=&quot;http://www.zkoss.org&quot;&gt;ZK&lt;/a&gt; on top of Gmarker." />
        </gmaps>
    </div>

谢谢。

由于 ZK 运行 在服务器端,因此无法从那里获取位置。唯一的方法(正如上面 Malte 的评论中已经提到的)是使用 html5 geolocation api.

可以使用自定义事件轻松地将从那里获得的信息发送回服务器。我创建了这个 zk fiddle example 来展示如何从客户端触发事件,以及如何在您的作曲家中收听它。然后您可以将坐标设置到 Gmaps 组件,或者像我那样动态创建一个新的 Gmaps 组件。

请注意:zkfiddle 目前不在 HTTPS 下 运行,因此我使用虚拟坐标和 setTimeout 模拟一些延迟。但是,我还提供了代码以从 navigator.geolocation.getCurrentLocation 上面刚刚注释掉的一行中请求坐标。

罗伯特