我是一个新的 phonegap 用户,我正在尝试我在 youtube 上找到的指南针教程。
一切似乎都运行良好,只是罗盘的指针不动。
好吧,我认为我的 addEventListener 无法正常工作。
我也不确定为什么。
有什么帮助吗?
我的项目文件结构
链接到我的项目文件以便于查看
https://docs.google.com/file/d/0B7-DKery9JL4Q2VWMUlaY08tREk/edit?usp=sharing
我的代码位于下面。
MainActivity.java
package com.example.compass;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
索引.html
<!DOCTYPE html>
<html>
<title> </title>
<head>
<style>
#compass {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
#needle {
position: absolute;
top: 8px;
left: 98px;
z-index: 10;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<script src="jqRotate/jqRotate.js"></script>
</head>
<body onload="onLoad()">
<img src="http://nathanhedglin.us/images/compass.png" id="compass">
<img src="http://nathanhedglin.us/images/needle.png" id="needle">
</body>
</html>
<script>
function onDeviceReady() {
alert("Starting!");
startCompass(); //runs the startCompass function
}
function onLoad() {
alert("Loading!!");
document.addEventListener("deviceready", onDeviceReady, false); //creates a listener that waits for the deviceready event and then fires onDevice ready function
}
function startCompass() {
var options = {
frequency : 50 //sets the compass heading to be updated every 50 milliseconds
};
navigator.compass.watchHeading(onSuccess, onError, options); //gets the compass heading from device and passes heading to onSuccess function
alert("Compass starting!!");
}
function onSuccess(heading) {//device s magnetic heading is passed to heading variable
$("#needle").rotate(-heading.magneticHeading); //jquery function that gets the #needle id and rotates it minus the current heading
}
function onError(compassError) { //if there is an error the error will be passed to onError in the compassError variable
alert('Compass error: ' + compassError.code); //displays an alert with the error code
}
</script>