当GPS关闭时,为什么Lat / Lng始终为0/0?

时间:2014-12-13 05:19:42

标签: android gps

我想简单地获取设备的Lat / Lng一次,以便为该位置运行搜索查询。鉴于此,我不需要继续询问位置,我只需要在创建活动时获得准确的位置(因此不是旧的存储位置)。

我的问题是,当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 个答案:

答案 0 :(得分: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);
}

然后使用location strategies doc。

中的isBetterLocation对两者进行比较