【问题标题】:Unable to get current latitude and longitude of current location in Android无法在Android中获取当前位置的当前纬度和经度
【发布时间】:2011-06-20 13:36:45
【问题描述】:

我使用下面的代码来获取用户的当前位置。此代码在模拟器中工作,但是当我在移动设备中使用此应用程序时,它无法提供当前的纬度和经度。 我在android清单文件中使用<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />这个代码。

请给我解决方案...我的代码如下

public class MenuPage extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menupage);


     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     LocationListener locListener = new MyLocationListener();
     locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locListener);



}

/* MyLocationListener 类的开始,用于获取用户的当前位置 */

public class MyLocationListener implements LocationListener

{

    @Override

    public void onLocationChanged(Location crurrentLocation)

    {
          Log.v("WHERE ","onLocationChanged()");
          crurrentLocation.getLatitude(); // get current latitude
          crurrentLocation.getLongitude(); // get current longitude
           longitude=crurrentLocation.getLongitude();
           latitude=  crurrentLocation.getLatitude();

           Log.v("WHERE ","onLocationChanged()  Latitude="+latitude);   
           Log.v("WHERE ","onLocationChanged()  Longitude="+longitude); 
            Toast.makeText(getApplicationContext(),"Latitud="+crurrentLocation.getLatitude(),Toast.LENGTH_SHORT).show(); 
            Toast.makeText(getApplicationContext(),"Longitud="+crurrentLocation.getLongitude(),Toast.LENGTH_SHORT).show();

            send_current_location = new Send_Current_location_ToServer();
      // Send current location to server
            String server_message =  send_current_location.sendData(userid,latitude,longitude);    
         //   String text ="My current location is: " + "Latitud = " + crurrentLocation.getLatitude() + "Longitud = " + crurrentLocation.getLongitude();
         //   Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();

    }

    @Override

    public void onProviderDisabled(String provider)

    {
          Log.v("WHERE ","onProviderDisabled()");
    Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();

    }

    @Override

    public void onProviderEnabled(String provider)

    {
         Log.v("WHERE ","onProviderEnabled()");

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

    }

    @Override

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

    {
         Log.v("WHERE ","onStatusChanged()");
    }

}/* End of Class MyLocationListener */

}

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    2.3 版模拟器中存在与位置服务有关的错误。尝试更改为 2.1 以测试您的代码。

    有关定位服务的更多信息,请使用this。

    请参阅here 了解错误的状态。

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2011-08-31
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2013-07-05
      相关资源
      最近更新 更多