我正在通过phonegap使用用户的地理位置。这个例子如下所示。 (机器人)
// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function(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/>' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy +'<br/>' +
'Heading: ' + position.coords.heading +<br/>' +
'timestamp: ' + position.timestamp +<br/>';
};
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
是否可以(使用phonegap)在错误功能上打开对话框,这将导致我们进行位置设置(用户将能够访问他的位置)而不是警报,因为它是在谷歌地图android应用程序中完成的(截图下方)?
答案 0 :(得分:0)
我正在做你正在说的话,创建对话框,然后将它们发送到设置中;我假设您已经设法在apropos时间调用onError fcn,那么
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.loc_man));
builder.setMessage(getString(R.string.ask_for_gps));
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getBaseContext(), getString(R.string.gps_no), Toast.LENGTH_SHORT).show();
finish();
}
});
builder.create().show();
你还需要做的就是从你的javascript中调用这个函数...哦,这很有趣,但是你很快就会使用很多非常酷的java概念而且我会这样做。我只与Cordova合作,但这应该都是一样的=]请记住,如果你在我的代码中看到一些未定义的变量,你应该认为它是一个很好的。
所以,让我们说你确定这段代码出错了,让我们组成一个接口名称; &#39;的Android&#39;是一个非常合乎逻辑的
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
Android.TurnOnTuneIn();
}
现在回到你的Java中,围绕你的phonegap应用程序找到它,无论它在哪里都可以找到WebView&#39 ;;如果它像我的cordova实现一样,它会在某个地方有webview.loadUrl()。打开定义此类的文件;这是编辑/放置我们正在处理的所有java的问题。在公共类PhoneGapActivity扩展Activity&#39;之后,插入&#39;实现CordovaInterface&#39; (我认为不是PhoneGapInterface);如果你的IDE在实现抽象方法的选项上给你一个错误,那就捣乱了。
确保WebView是类中的一个字段,而不是方法中的变量,然后
//CordovaWebView should work, but maybe it needs to be PhoneGapWebView, you're smart, you'll figure it out =]
webView = (CordovaWebView) findViewById(R.id.data_window);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); //this always seemed like a good idea to me
webView.addJavascriptInterface(new JSInterface(this), "Android"); //see the interface name? =]
//this line should already be in this class, just re-use it
webView.loadUrl("file:///"+getContext().getFilesDir().toString()+"/index.html");
现在制作该界面:
public class JSInterface {
// these fcns are exposed to the WebView
private Context context;
public JSInterface(Context context)
{
context = context;
}
@JavascriptInterface
public void doEchoTest(String echo)
{
//A useful test; I usually use it in a webViewClient that runs the function that calls this in my javascript with this:
//webView.loadUrl("javascript:echo('"echo!!!"');"); //this exact formatting
//but webViewClient is outside the scope of your question and would just confuse the
//issue; however, you may need to implement
//webViewClient with an delaying asynctask to seperatly run the js you loaded with the webpage,
//again, I used to do this ground-up, and phoneGap Might Just Work.
Toast.makeText(context, echo, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void TurnOnTuneIn
{
aMethodThatHasThatFirstBitOfCodeIPublished();
}
}
我喜欢接口=]
其余部分对于你的目的而言是相当的样板(尽管如此玩起来很有趣!)你可能不需要太多(如果有的话)。如果你注意到我提出的方法没有以你想要的方式返回,你可以使用startActivityForResult方法做同样的事情,我敢打赌它会很好地工作;注意超级。&#39;称之为Override;你只需要Intent(就像我向你展示的那样)和一些用于接收结果的参考号...再次超出了问题的范围。另外,我支持ResultCallback b / c我正在使用它的人,但是我自己也没有使用它。
@Override
public Activity getActivity(){
return this;
}
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
this.activityResultCallback = plugin;
}
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
@Override
public void cancelLoadUrl(){
// no op
}
@Override
public Object onMessage(String id, Object data) {
LOG.d("is", "onMessage(" + id + "," + data + ")");
if ("exit".equals(id)) {
super.finish();
}
return null;
}
@Override
public void onPause() {
Method pause = null; // Pauses the webview.
try {
pause = WebView.class.getMethod("onPause");
}
catch (SecurityException e) { }
catch (NoSuchMethodException e) { }
if (pause != null) {
try { pause.invoke(webView);
}
catch (InvocationTargetException e) { }
catch (IllegalAccessException e) { }
}
else {
// No such method. Stores the current URL.
suspendUrl = webView.getUrl(); // And loads a URL without any processing.
webView.loadUrl("file:///android_asset/nothing.html");
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
Method resume = null; // Resumes the webview.
try {
resume = WebView.class.getMethod("onResume");
}
catch (SecurityException e) { }
catch (NoSuchMethodException e) { }
if (resume != null) {
try {
resume.invoke(webView);
}
catch (InvocationTargetException e) { }
catch (IllegalAccessException e) { }
}
else if (webView != null) { // No such method. Restores the suspended URL.
if (suspendUrl == null) {
//this should be wherever you have your page; you can probably copy it from what is there now.
webView.loadUrl("file:///"+getContext().getFilesDir().toString()+"/index.html");
}
else {
webView.loadUrl(suspendUrl);
}
}
}
希望能让你更进一步;如果您还没有使用它,请记住使用版本控制,这样您就可以对编辑不顾一切!
gl hf
答案 1 :(得分:0)
我与你分享这款phonegap插件,可以让你这样做。我将该功能添加到该插件作为贡献者。 https://github.com/BastienL/GPSDetector