我正在开发基于位置的项目,我正在使用以下代码
我正在使用google api 8进行项目
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
TextView t = (TextView)findViewById(R.id.textView1);
try{
t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
}catch (Exception e) {
// TODO: handle exception
t.setText("cant find current location ");
}
这个代码在我的galaxy选项卡上运行正常,即使在htc
但是当我使用nexus时,它会为location返回null值。 我是否需要更改我的api级别,或者是否有任何针对galaxy nexus的特定要求 提前谢谢你:)
答案 0 :(得分:3)
请遵循代码行..
Step1: into your oncreate
LocationListener locationListener = new LocalLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Step2: into class body
/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
mapController_.animateTo(geoPoint);
Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
Log.i("onLocationChanged", text);
}
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
Log.i("onProviderDisabled", "GPS Disable");
}
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
Log.i("onProviderEnabled", "GPS Enable");
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}