准确的纬度和经度不归还?

时间:2014-12-01 05:14:13

标签: android

我想通过android找到用户的当前位置,但总是返回特定区域10米范围内的位置。它不会返回实际的地方。我使用google play services location api来代替当前位置。

我如何检索准确的纬度和经度?

1 个答案:

答案 0 :(得分:1)

试试这个。我希望它对你有所帮助

public class MainActivity extends Activity implements LocationListener {
    private static final String TAG = "MainActivity";
    LocationManager lm;
    private static TextView lt;
    private static TextView ln;
    private static TextView ac;
    private static WebView webview;
    String provider;
    Location l;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ln = (TextView) findViewById(R.id.lng);
        lt = (TextView) findViewById(R.id.lat);
        ac = (TextView) findViewById(R.id.acc);
        webview = (WebView) findViewById(R.id.webview);
        // get location service
        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSProviderEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean isNETProviderEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if( !isGPSProviderEnabled || !isNETProviderEnabled) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Location is off");  // GPS not found
            builder.setMessage("Location setting open"); // Want to enable?
            builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            });
            builder.setNegativeButton(android.R.string.no, null);
            builder.create().show();
            return;
        }
        Criteria c = new Criteria();
        // criteria object will select best service based on
        // Accuracy, power consumption, response, bearing and monetary cost
        // set false to use best service otherwise it will select the default
        // Sim network
        // and give the location based on sim network
        // now it will first check satellite than Internet than Sim network
        // location
        provider = lm.getBestProvider(c, false);

        // now you have best provider
        // get location
        l = lm.getLastKnownLocation(provider);
        if (l != null) {
            // get latitude and longitude of the location
            double lng = l.getLongitude();
            double lat = l.getLatitude();
            double acc = l.getAccuracy();
            // display on text view
            updateview(lng, lat, acc);
        } else {
            // Register the listener with the Location Manager to receive location updates
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, this);
            ln.setText("No Provider");
            lt.setText("No Provider");
            ac.setText("No Provider");
        }
    }

    // If you want location on changing place also than use below method
    // otherwise remove all below methods and don't implement location listener
    @Override
    public void onLocationChanged(Location l) {
        Log.v(TAG, "onLocationChanged");
        double lng = l.getLongitude();
        double lat = l.getLatitude();
        double acc = l.getAccuracy();
        updateview(lng, lat, acc);

    }

    private void updateview(final double lng, final double lat, final double acc) {
        Log.v(TAG, "updateview");
        Log.v(TAG, "lng:"+lng);
        Log.v(TAG, "lat:"+lat);
        Log.v(TAG, "acc:"+acc);
        ln.setText(String.valueOf(lng));
        lt.setText(String.valueOf(lat));
        ac.setText(String.valueOf(acc));
        String URL = "http://maps.google.com/maps/api/staticmap?center="+lat+","+lng+"&zoom=15&size=500x200&markers=color:red|"+lat+","+lng+"&sensor=false";
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.loadUrl(URL);
        webview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()) {
                  case MotionEvent.ACTION_UP:
                      String uri = String.format(Locale.getDefault(), "geo:%f,%f", lat, lng);
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        startActivity(intent);  
                        return v.performClick();
                  }
                return false;
            }
        });
    }

    @Override
    public void onProviderDisabled(String arg0) {
        Log.v(TAG, "onProviderDisabled"+arg0);
    }

    @Override
    public void onProviderEnabled(String arg0) {
        Log.v(TAG, "onProviderEnabled"+arg0);
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.v(TAG, "onStatusChanged"+arg0);
    }

这是XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#89b" >
<TextView
   android:id="@+id/textView1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_alignParentTop="true"
   android:layout_marginLeft="18dp"
   android:layout_marginTop="16dp"
   android:text="Longitude:" />
<TextView
   android:id="@+id/lng"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/textView1"
   android:layout_marginLeft="60dp"
   android:layout_marginTop="14dp"
   android:layout_toRightOf="@+id/textView1"
   android:text=""
   android:textSize="30sp" />
<TextView
   android:id="@+id/textView3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignRight="@+id/textView1"
   android:layout_below="@+id/lng"
   android:layout_marginTop="53dp"
   android:text="Latitude:" />
<TextView
   android:id="@+id/lat"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/lng"
   android:layout_below="@+id/textView3"
   android:text=""
   android:textSize="30sp" />
<TextView
   android:id="@+id/textView4"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignRight="@+id/textView3"
   android:layout_below="@+id/lat"
   android:layout_marginTop="53dp"
   android:text="Accuracy:" />
<TextView
   android:id="@+id/acc"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/lat"
   android:layout_below="@+id/textView4"
   android:text=""
   android:textSize="30sp" />

<WebView
    android:id="@+id/webview"
    android:layout_width="500dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/acc"
    android:layout_marginTop="22dp" />

</RelativeLayout>