IBM工作灯 - 如何使用谷歌地图API [英] IBM Worklight - How to use Google Maps API

查看:139
本文介绍了IBM工作灯 - 如何使用谷歌地图API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如何在IBM工作灯应用程序中使用地理位置(使用谷歌地图API)

我很困惑,我怎样才能使利用GPS应用自动检测我的位置?结果
又如何让应用程序始终实时跟踪用户的位置?

这是我的JavaScript:

  VAR X =的document.getElementById(演示);功能的getLocation()
  {
  如果(navigator.geolocation)
    {
      警报(尔雅);
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  其他{x.innerHTML =地理位置是此浏览器不支持。;}
  }
功能showPosition(位置)
  {
  x.innerHTML =纬度:+ position.coords.latitude +
  < BR>经度:+ position.coords.longitude;
  }

的HTML:

 <!DOCTYPE HTML>
< HTML和GT;
        < HEAD>
            <间的charset =UTF-8>
            <标题> HTMLGeoLocation< /标题>
            &所述;元名称=视口内容=宽度=设备宽度,初始规模= 1.0,最大规模= 1.0,最小规模= 1.0,用户可扩展= 0>
            <链接rel =快捷方式图标的href =图像/ favicon.png>
            <链接rel =苹果触摸图标的href =图像/苹果触摸的icon.png>
            <链接rel =stylesheet属性HREF =CSS / HTMLGeoLocation.css>
            <脚本方式>窗口$ = window.jQuery = WLJQ;< / SCRIPT>
        < /头>
        <身体ID =内容的风格=显示:无;>
            <! - 应用程序的UI放在这里 - >
            < p n =演示>点击该按钮,让您的坐标:< / P>
<按钮的onclick =的getLocation()>试试看< /按钮>
            <脚本SRC =JS / initOptions.js>< / SCRIPT>
            <脚本SRC =JS / HTMLGeoLocation.js>< / SCRIPT>
            <脚本SRC =JS / messages.js>< / SCRIPT>
        < /身体GT;
< / HTML>


解决方案

我没有使用 IBM工作灯,但我知道这是一个用于开发移动应用程序的工具。这里没有什么做地理定位。

这纯粹是 HTML5 的手和谷歌地图API

 <脚本类型=文/ JavaScript的SRC =htt​​p://maps.google.com/maps/api/js?sensor=false>< /脚本>

希望你知道的JavaScript ,上面的脚本是谷歌地图API的JavaScript

 如果(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(位置); //这将返回的纬度/经度
  } // 当前位置
其他{
    //句柄,如果失败GeoLocation中
  }

您可以获得当前的位置只有当用户允许你。你不能强迫得到地理定位。

地理位置文档转到 谷歌地图API的。

既然你提到的 GPS 后,请通过下面重点介绍基本需要的段落。


  

指定传感器参数


  
  

在谷歌地图API的使用需要你说明是否你
  应用程序正在使用的传感器(如GPS定位器),以确定
  用户的位置。这是作为移动设备尤其重要。
  应用程序必须通过要求传感器参数标签
  包括Google地图API的JavaScript code时,指示是否
  您的应用程序正在使用传感器设备。


  
  

应用程序,它通过传感器确定用户的位置,必须通过
  传感器= true,则加载地图API的JavaScript时。


希望你能理解。

My question is: How to use geolocation (using Google Maps API) in an IBM Worklight application.

I am confused as to how can I make the application auto-detect my location using GPS?
Also how to make the application always track the user position in real time?

This is my JavaScript:

var x=document.getElementById("demo");

function getLocation()
  {
  if (navigator.geolocation)
    {
      alert("ya");
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }

The HTML:

<!DOCTYPE HTML>
<html>
        <head>
            <meta charset="UTF-8">
            <title>HTMLGeoLocation</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
            <link rel="shortcut icon" href="images/favicon.png">
            <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
            <link rel="stylesheet" href="css/HTMLGeoLocation.css">
            <script>window.$ = window.jQuery = WLJQ;</script>
        </head>
        <body id="content" style="display: none;">
            <!--application UI goes here-->
            <p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
            <script src="js/initOptions.js"></script>
            <script src="js/HTMLGeoLocation.js"></script>
            <script src="js/messages.js"></script>
        </body>
</html>

解决方案

I haven't used IBM Worklight, but I know it is a tool for developing mobile applications. Here there is nothing to do with geolocation.

It purely in the hands of HTML5 and Google maps API.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

Hope you know Javascript, the above script is Google Maps Javascript API.

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(position); // this will return lat/lng of 
  }                                                     // current location
else {
    //Handle if GeoLocation fails
  }

You can get the current location only if user allows you. You can't force to get the geolocation.

Go through Geolocation documentation of Google maps API.

Since you mentioned about GPS, please go through the below highlighted paragraph essentially required.

Specifying the Sensor Parameter

Use of the Google Maps API requires that you indicate whether your application is using a sensor (such as a GPS locator) to determine the user's location. This is especially important for mobile devices. Applications must pass a required sensor parameter to the tag when including the Maps API javascript code, indicating whether or not your application is using a sensor device.

Applications that determine the user's location via a sensor must pass sensor=true when loading the Maps API JavaScript.

Hope you understand.

这篇关于IBM工作灯 - 如何使用谷歌地图API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆