2014-03-05 47 views
5

我通过phonegap使用用户的地理位置。如下所示。 (ANDROID)使用手机开放Android位置设置

// 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应用程序(截图下面)? like this

+0

没有ü添加ACCESS_FINE_LOCATION许可清单文件,当然 –

+0

是的,我有机会获得用户的位置,我的问题是一个插件像错误 – David

+0

使用打开设置页面:https://github.com/romainperruchon/ gpssettings/BLOB /主/ src目录/安卓/ COM/dataforpeople /插件/ GpsSettings.java。 –

回答

0

我只是按照你所说的去做,创建对话框并发送给设置;我假设你已经设法拜访了中肯的时间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调用这个函数......哦,这很有趣,B/c你会一次性使用很多非常酷的java概念,我只和Cordova一起工作,但这应该都是一样的=]请记住,如果你在我的代码中看到一些未定义的变量,你应该可能认为它是一个领域。

因此,假设您确定此代码遇到错误,我们来编写一个接口名称;对“Android”是一个非常合乎逻辑的一个

function onError(error) { 
    alert('code: ' + error.code + '\n' + 
      'message: ' + error.message + '\n'); 
    Android.TurnOnTuneIn(); 
} 

现在又回到你的Java,在你的PhoneGap的应用grep来找到哪里有“的WebView”;如果它与我的cordova实现类似,它会在某处放置webview.loadUrl()。打开定义这个类的文件;这是编辑/放置我们正在处理的所有java的问题。在'公共类PhoneGapActivity扩展Activity'之后,插入'implements CordovaInterface'(我认为不是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方法来做同样的事情,我敢打赌它会很好地工作;请注意'超级'。调用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

+0

我根本不知道java,所以这是我在phonegap上的第一步,我不知道如何处理你给我的代码,你能否详细解释 – David

+0

kk,请参阅edit =] –