从函数外调用位置坐标[英] Call location coordinates from outside the function

本文是小编为大家收集整理的关于从函数外调用位置坐标的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我尝试从getCurrentPosition访问LAT,以便我可以创建一个与API一起使用的URL.如果您从函数内部调用它,或者在警报上设置超时(功能之外),则可以正常工作,因为当前位置需要一些时间来确定坐标.如何访问坐标变量和/或URL一个?谢谢

  var coords1;
  var coords2;

  if (Ti.Geolocation.locationServicesEnabled) {
  Titanium.Geolocation.purpose = 'Get Current Location';
  Titanium.Geolocation.getCurrentPosition(function(e) {
    if (e.error) {
        Ti.API.error('Error: ' + e.error);
    } else {
        coords1 = e.coords.longitude;
        coords1 = e.coords.latitude;
    }
  });
  } else {

    alert('Please enable location services');
  } 


  var url = "http://www.mywebsite.com/api/return-latlong/"+coords1+"/"+coords2;
  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this function is called when data is returned from the server and available for use
        // this.responseText holds the raw text return of the message (used for text/JSON)
        // this.responseXML holds any returned XML (including SOAP)
        // this.responseData holds any returned binary data
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        // this function is called when an error occurs, including a timeout
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  // in milliseconds
   });
   xhr.open("GET", url);
   xhr.send();  // request is actually sent with this statement


  alert(url);

推荐答案

不确定我真的会遇到您的问题,但是您可以为您的API HTTP请求使用功能,并在获得当前位置的坐标时致电,如下所示:

if (Ti.Geolocation.locationServicesEnabled) {
  Titanium.Geolocation.purpose = 'Get Current Location';
  Titanium.Geolocation.getCurrentPosition(function(e) {
    if (e.error) {
        Ti.API.error('Error: ' + e.error);
      } else {

        var latitude = e.coords.latitude;
        var longitude = e.coords.longitude;

        // make the http request when coords are set
        apiCall(latitude, longitude);
    }
  });
} else {
    alert('Please enable location services');
}

function apiCall(lat, long) {
  var url = "http://www.mywebsite.com/api/return-latlong/" + lat + "/" + long;
  alert(url);

  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  
  });

  xhr.open("GET", url);
  xhr.send(); 
}

hth.

本文地址:https://www.itbaoku.cn/post/1938211.html

问题描述

I'm try to access the lat and long from getCurrentPosition so that I can create a URL to use with an API. This is working if you call it from within the function or if you set a timeout on an alert(outside the function), as the current position takes a few moments to determine the coordinates. How can I access coords variables and/or the URL one? Thank you

  var coords1;
  var coords2;

  if (Ti.Geolocation.locationServicesEnabled) {
  Titanium.Geolocation.purpose = 'Get Current Location';
  Titanium.Geolocation.getCurrentPosition(function(e) {
    if (e.error) {
        Ti.API.error('Error: ' + e.error);
    } else {
        coords1 = e.coords.longitude;
        coords1 = e.coords.latitude;
    }
  });
  } else {

    alert('Please enable location services');
  } 


  var url = "http://www.mywebsite.com/api/return-latlong/"+coords1+"/"+coords2;
  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this function is called when data is returned from the server and available for use
        // this.responseText holds the raw text return of the message (used for text/JSON)
        // this.responseXML holds any returned XML (including SOAP)
        // this.responseData holds any returned binary data
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        // this function is called when an error occurs, including a timeout
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  // in milliseconds
   });
   xhr.open("GET", url);
   xhr.send();  // request is actually sent with this statement


  alert(url);

推荐答案

not sure I really get your problem, but you can use a function for your api http request, and call it when you obtain the current position's coords, something like the following:

if (Ti.Geolocation.locationServicesEnabled) {
  Titanium.Geolocation.purpose = 'Get Current Location';
  Titanium.Geolocation.getCurrentPosition(function(e) {
    if (e.error) {
        Ti.API.error('Error: ' + e.error);
      } else {

        var latitude = e.coords.latitude;
        var longitude = e.coords.longitude;

        // make the http request when coords are set
        apiCall(latitude, longitude);
    }
  });
} else {
    alert('Please enable location services');
}

function apiCall(lat, long) {
  var url = "http://www.mywebsite.com/api/return-latlong/" + lat + "/" + long;
  alert(url);

  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  
  });

  xhr.open("GET", url);
  xhr.send(); 
}

Hth.