2012-07-26 85 views
0

我想获取当前位置的经纬度。我为此使用了LocationManager,但它显示的位置为空,纬度和经度为0.0。我已经在清单和GPS连接中使用了所需的权限。使用LocationManager时,位置显示为空

代码:

LocationdemoActivity.java

public class LocationdemoActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener); 
     mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); 

     Button buttonLocation = (Button) findViewById(R.id.buttonLocation); 
     buttonLocation.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 

       Toast.makeText(
         getApplicationContext(), 
         "loc:" + MyLocationListener.location + "latitude " 
           + MyLocationListener.latitude + "longitude " 
           + MyLocationListener.longitude, 5000).show(); 

      } 

     }); 

    } 
} 

MyLocationListener.java

​​

权限清单中:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/> 

什么问题?

+0

,你正在检查这个模拟器或设备? – Akram 2012-07-26 06:54:23

+0

确保网络或GPS提供商在位置设置中进行操作。如果你正在使用模拟器,你将不得不使用DDMS模拟你的位置。 – 2012-07-26 07:57:55

+0

@Akki Iam检查设备和模拟器..但仍然没有得到 – Thirupathig 2012-07-26 09:00:27

回答

0

尝试做这样的事情

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener); 

location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

Button buttonLocation = (Button) findViewById(R.id.buttonLocation); 

buttonLocation.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 

       Toast.makeText(getApplicationContext(), 
          "loc:" + location + "latitude " 
            + location.latitude + "longitude " 
            + location.longitude, 5000).show(); 

       } 

      }); 
+1

嘿它在设备上正常工作....感谢兄弟....我怀疑如果我移动到另一个位置,拉特和长期将改变正确...我想要我的监听器代码,因为onLocationChanged()中没有变量和空代码。我认为这样不会有问题 – Thirupathig 2012-07-26 10:58:33

相关问题