phonegap相机和地理定位不在android中一起工作

时间:2014-06-05 20:12:04

标签: cordova geolocation

Pls,我正在开发一款利用phonegap相机和地理定位的Android应用程序。到目前为止,我可以捕获图片,但是一旦我包含应该启用地理定位的代码,捕获就会失败并且地理定位不会出现。下面是失败的代码:

<!DOCTYPE html>
<html>
<head>
<title>Submit form</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">

var pictureSource;   // picture source
var destinationType; // sets the format of returned value

// Wait for device API libraries to load
//
document.addEventListener("deviceready",onDeviceReady,false);

  // device APIs are available
  //
  function onDeviceReady() {
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
  }

// A button will call this function
  //
  function getPhoto() {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
    destinationType: destinationType.FILE_URI });
  }

 function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
                        'Longitude: '+ position.coords.longitude + '<br />' +
                        'Altitude: ' + position.coords.altitude + '<br />' +
                        'Accuracy: ' + position.coords.accuracy + '<br />' +
                        'Timestamp: ' + position.timestamp + '<br />';
  }


  // Called when a photo is successfully retrieved
  //
  function onPhotoURISuccess(imageURI) {

    // Show the selected image
    var smallImage = document.getElementById('smallImage');
    smallImage.style.display = 'block';
    smallImage.src = imageURI;
  }

  function uploadPhoto() {

    //selected photo URI is in the src attribute (we set this on getPhoto)
    var imageURI = document.getElementById('smallImage').getAttribute("src");
    if (!imageURI) {
        alert('Please select an image first.');
        return;
    }

    //set upload options
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType = "image/jpeg";
    options.chunkedMode = false;

    options.params = {
        firstname: document.getElementById("firstname").value,
        lastname: document.getElementById("lastname").value

    }


    options.headers = {
      Connection: "close"
    };

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI("http://mydomain.com/upload.php"),win,fail,options);
}

// Called if something bad happens.
//
function onFail(message) {
  console.log('Failed because: ' + message);
}

function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    alert("Response =" + r.response);
    console.log("Sent = " + r.bytesSent);
}

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

</script>
</head>
<body>

    <button onclick="getPhoto();">Select Photo:</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" /><br>
    <p id="geolocation">Finding geolocation...</p><br>

<form id="regform">
    First Name: <input type="text" id="firstname" name="firstname"><br>
    Last Name: <input type="text" id="lastname" name="lastname"><br>

    <input type="button" id="btnSubmit" value="Submit" onclick="uploadPhoto();">
</form>
</body>
</html>

感谢您的预期帮助。

1 个答案:

答案 0 :(得分:0)

在添加事件侦听器之前,您需要添加在文档加载后调用的函数。

示例:

<body onload="onLoad()">

然后:

function onLoad() {
    document.addEventListener("deviceready",onDeviceReady,false);
}

原因是您在事件触发后立即调用地理位置API,但事件可以在将数据写入的html元素之前触发。

如果你先等待身体加载,那么JS就永远不会与html进行交互。