如何获取android设备的纬度和经度

如何获取android设备的纬度和经度,android,Android,我在安卓系统工作。我已经做了一个程序来获取纬度和经度 这是我的节目 package com.ram.currentlocation; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager;

我在安卓系统工作。我已经做了一个程序来获取纬度和经度

这是我的节目

package com.ram.currentlocation;

import android.app.Activity;    
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Location_Gps extends Activity

{

    TextView ll;
    TextView lt;

    static double lati;
    static double longi;

/** Called when the activity is first created. */

 @Override



public void onCreate(Bundle savedInstanceState)
{

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);


  /* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager = 

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

  LocationListener mlocListener = new MyLocationListener();



 mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
  }



 /* Class My Location Listener */

    public class MyLocationListener implements LocationListener


    {

    @Override

    public void onLocationChanged(Location loc)

    {


        lati= loc.getLatitude();

        longi=loc.getLongitude();

    String Text = "My current location is:" +

    "Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();


    Toast.makeText( getApplicationContext(), Text,  Toast.LENGTH_SHORT).show();


       }


    @Override

    public void onProviderDisabled(String provider)

    {

    Toast.makeText( getApplicationContext(),"Gps Disabled",   Toast.LENGTH_SHORT ).show();

    }


    @Override

    public void onProviderEnabled(String provider)

    {

    Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

    }


    @Override

    public void onStatusChanged(String provider, int status, Bundle extras)

    {


    }
    public Location_Gps getLocationCordinates()
    {
          Location_Gps location_Gps =new Location_Gps();

        return location_Gps;

    }

    }
    //---



}
但它第一次给出了纬度和经度0,0。
每当我移动我的设备,它就会给出一些分数。请建议我第一次获取纵坐标时应该做什么。

第一次您可以使用
getLastKnownLocation
这样的功能:

   Location locations = _locationManager.getLastKnownLocation(_provider);
   List<String>  providerList = _locationManager.getAllProviders();
   if(null!=locations && null!=providerList && providerList.size()>0) {
   double _longitude = locations.getLongitude();
   double _latitude = locations.getLatitude();
   }
Location locations=\u locationManager.getLastKnownLocation(\u provider);
List providerList=\u locationManager.getAllProviders();
if(null!=位置&&null!=providerList&&providerList.size()>0){
double _longitude=locations.getLongitude();
双纬度=位置。getLatitude();
}

在Android中,您无法立即获取GPS坐标。只有当其他人在您之前请求了它们,或者如果您的GPS模块打开了一段时间,您才有机会在第一次呼叫时获得它们。所以,听GPS模块的声音,直到你们得到精确的坐标,这是一个常规的做法

有关代码示例,请参阅官方android文档:
Yopu有ti使用定位服务-有很多。
每个可用的位置服务都有特定的含义,如能耗、启动时间、粗糙度等。我发现Reto Maier的博客条目很有用:
这是一个已知的问题,是由于GPS芯片组工程师犯了愚蠢的错误造成的。还要注意的是,几乎总是知道起始点有点抖动

  • 检查精度是否足够好。使用
  • 检查
    onLocationChanged(位置loc)
    中的loc是否为空
  • 使用
    GPSStatus.Listener
    获取数据。在此之前的任何位置实际上都是无用的
  • 了解是否已实现修复
有人可以更改我的上述程序以进行更正吗。。?