2014-12-13 45 views
0

我試圖簡單地獲取該設備的緯度/經度,以便運行該位置的搜索查詢。鑑於此,我不需要繼續詢問位置,在創建活動時我只需要一個準確的位置(所以不是舊的存儲位置)。GPS關閉時Lat/Lng爲何始終爲0/0?

我的問題是,當GPS關閉時,Lat/Lng始終爲0/0 ...所以提供商/網絡位置服務顯然因爲某些原因不起作用。當GPS開啓時...它工作得很好,我得到了Lat/Lng。

這裏是我的活動:

package com.app.abc123; 

import android.location.Criteria; 
import android.location.LocationManager; 
import android.support.v7.app.ActionBarActivity; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.drawable.AnimationDrawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class Search_results extends ActionBarActivity { 

private WebView mWebView; 
private AnimationDrawable frameAnimation; 
String provider; 
LocationManager lm; 
android.location.Location l; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search_results); 
    overridePendingTransition(R.anim.activity_in, R.anim.activity_out); 
    // Get the message from the intent 
    Intent intent = getIntent(); 
    String intentLat = intent.getStringExtra("LAT"); 
    String intentLng = intent.getStringExtra("LNG"); 
    String intentQuery = intent.getStringExtra("QUERY"); 

    setTitle(intentQuery); 

    if ((Double.valueOf(intentLat) == 0) || (Double.valueOf(intentLng) == 0)) { 
     lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     Criteria c = new Criteria(); 
     provider = lm.getBestProvider(c, true); 
     l = lm.getLastKnownLocation(provider); 
     if (l != null) { 
      intentLat = String.valueOf(l.getLatitude()); 
      intentLng = String.valueOf(l.getLongitude()); 
     } 
    } 

    mWebView = (WebView) findViewById(R.id.activity_search_results); 
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setAllowFileAccess(true); 
    mWebView.addJavascriptInterface(new WebAppInterface(this), "Android"); 
    mWebView.loadUrl("http://abc123.azurewebsites.net/search.php?lat=" + intentLat + "&lng=" + intentLng + "&query=" + intentQuery); 
    mWebView.setWebViewClient(new WebViewClient() { 

     ... 
     Web view stuff 
     ... 

    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.search_results, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle presses on the action bar items 
    switch (item.getItemId()) { 
     case R.id.action_refresh: 
      mWebView.loadUrl("javascript: refresh_page();"); 
      return true; 
     default: 
      finish(); 
      overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out); 
      return true; 
    } 
} 

@Override 
public void onBackPressed() { 
    // finish() is called in super: we only override this method to be able to override the transition 
    super.onBackPressed(); 

    overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out); 
} 

}

是的,我確實有正確的權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 

回答

1

getBestProvider只是說,它Returns the name of the provider that best meets the given criteria。這並不一定意味着提供商有修正。

location strategies文檔有關於獲取當前位置的最佳估計的很好的部分。

Location lastGpsLocation = null; 
Location lastNetworkLocation = null; 
List<String> enabledLocationProviders = mLocationManager.getProviders(true); 

if (enabledLocationProviders.contains(LocationManager.GPS_PROVIDER)) { 
    lastGpsLocation = mLocationManager 
     .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
} else if (enabledLocationProviders.contains(LocationManager.NETWORK_PROVIDER)) { 
    lastNetworkLocation = mLocationManager 
     .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
} 

然後比較這兩個使用isBetterLocationlocation strategies文檔。

+0

啊,好的,非常有趣,我會試試看。 – rblythe 2014-12-13 06:10:30

相關問題