相关疑难解决方法(0)

navigator.geolocation.getCurrentPosition有时候工作有时不会

所以我使用navigator.geolocation.getCurrentPosition jammy有一个相当简单的JS.

$(document).ready(function(){
  $("#business-locate, #people-locate").click(function() {
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
  });

  navigator.geolocation.getCurrentPosition(foundLocation, noLocation);

  function foundLocation(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var userLocation = lat + ', ' + lon;
    $("#business-current-location, #people-current-location").remove();
    $("#Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='business-current-location' id='business-current-location' value='"+userLocation+"' />");
    $("#people-Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='people-current-location' id='people-current-location' value='"+userLocation+"' />");
  }
  function noLocation() {
    $("#Near-Me").watermark("Could not find location");
    $("#people-Near-Me").watermark("Could not find location");
  }
})//end DocReady
Run Code Online (Sandbox Code Playgroud)

基本上这里发生的是我们得到当前位置,如果得到的话,两个"水印"被放置在两个字段中,表示"当前位置",两个隐藏字段用lat-long数据作为它们的值(它们被删除)在一开始所以他们不会每次都重复).还有两个按钮,它们具有与之相关的点击功能,可以执行相同的操作.不幸的是,每三次左右,它都有效.这有什么问题???

javascript jquery geolocation

207
推荐指数
11
解决办法
25万
查看次数

是否可以在javascript中检查null内联?

我有一个函数可以解析Google Maps API的地址组件, JSON然后返回城市/地区/路线名称.

getAddressComponent()回报null,如果它不能找到问题的关键.

let route = getAddressComponent(addressComponents, 'route').value.long_name;

所以,让我们说它没有找到密钥,然后我Error: TypeError: Cannot read property 'long_name' of undefined明显得到了一个,因为它没有定义.

如何null在条件方法以外的javascript中检查(a === null)

我怎么能这样简单地检查一下 ?

编辑:安全导航操作员

let route = getAddressComponent(addressComponents, 'route')?.value.long_name;

如果它不存在,它可能设置routenull而不是抛出错误?

javascript null

11
推荐指数
4
解决办法
2万
查看次数

检查用户设备的 GPS 是否开启

我正在使用 jQuery Mobile 和 PHP 开发一个应用程序。我没有使用 Phonegap 或其他框架。我需要找到用户的geolocation. 如果用户设备的 GPS 关闭,我将无法获取位置。现在我需要查找用户设备的 GPS 是否打开或关闭。

这就是我现在使用的。

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  var lat=position.coords.latitude;
  var long=position.coords.longitude;
}
Run Code Online (Sandbox Code Playgroud)

javascript php gps jquery-mobile

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×3

geolocation ×1

gps ×1

jquery ×1

jquery-mobile ×1

null ×1

php ×1