Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未返回准确的纬度和经度?_Android - Fatal编程技术网

Android 未返回准确的纬度和经度?

Android 未返回准确的纬度和经度?,android,Android,我想通过android找到用户的当前位置,但总是返回特定区域10米范围内的位置。它不会返回实际位置。我正在使用google play services location api查找当前位置 我是如何获取准确的纬度和经度的?试试这个。我希望这对你有很大帮助 public class MainActivity extends Activity implements LocationListener { private static final String TAG = "MainActivi

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


我是如何获取准确的纬度和经度的?

试试这个。我希望这对你有很大帮助

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>


lat Lang中有什么问题?您尝试过使用GPS吗?是的,我已经尝试过使用GPS,但返回的数据不准确。实际上,我需要构建一个应用程序,该应用程序将形成一个包含四组lat Long的多边形,然后在该多边形中找到一个位置。我通过移动四个不同的位置来收集四个lat Long,然后在多边形的位置之间,我发现返回的值位于多边形的外部。我们正在跟踪的横向长度如下22.5682203,88.4346739 22.568792,88.4344252 22.5688278,88.4344034 22.5688277,88.4344033,在形成的多边形内部,我发现当前的横向长度为22.5705803和88.4316084,但返回的是polygonI的外部。我相当肯定,这一切都取决于您的设备在任何给定时间可以看到多少颗GPS卫星时间当你使用谷歌地图或其他提供位置信息的地图时,你会得到不同的结果吗?