我已经实现了示例应用程序以获取当前位置纬度经度。如果我在模拟器中午餐我的应用程序并且我在eclipse中从Emulator Controls发送经度和经度然后我从模拟器控件获取当前位置纬度和经度。如果我在真实设备中吃同样的应用程序,然后我无法获得当前位置的纬度和经度
我已经实现了获取当前位置纬度和经度的代码,如下所示:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 11, 11, mlocListener);
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
Log.v("11111","Latitude :"+loc.getLatitude());
Log.v("22222","Longitude :"+ loc.getLongitude());
}
}
从上面的代码我没有在真正的Android设备中获得当前位置纬度和经度。
如何获取真实设备的当前位置纬度和经度?
请任何人帮助我
答案 0 :(得分:4)
GPS_PROVIDER无法在绑定位置使用。因此,如果gps_provider已启用但您获得null位置,则可以将提供程序从gps替换为NETWORK_PROVIDER。
答案 1 :(得分:2)
prasad试试这段代码..通过使用这个我成功地获得了很长的
private Location location = null;
private LocationManager locationManager = null;
locationManager = (LocationManager) context.getSystemService (Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
String providerName = locationManager.getBestProvider(locationCritera,
true);
location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
currentLocation = context.getSharedPreferences(PREFS_NAME, 0);
editor = currentLocation.edit();
}
public String getCurrentLatitude() {
try {
if (!currentLocation.getString("currentLatitude", "")
.equalsIgnoreCase(""))
return currentLocation.getString("currentLatitude", "");
else if (location.getLatitude() != 0.0)
return Double.toString(location.getLatitude());
else
return "0.0";
} catch (Exception e) {
e.printStackTrace();
}
return "0.0";
}
public String getCurrentLongitude() {
try {
if (!currentLocation.getString("currentLongitude", "")
.equalsIgnoreCase(""))
return currentLocation.getString("currentLongitude", "");
else if (location.getLongitude() != 0.0)
return Double.toString(location.getLongitude());
else
return "0.0";
} catch (Exception e) {
e.printStackTrace();
}
return "0.0";
}
答案 2 :(得分:1)
答案 3 :(得分:1)
在MyLocationListener中覆盖这些方法。这样您就可以跟踪您的GPS状态。检查一下你是否能解决问题
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
答案 4 :(得分:1)
在真实设备上,您必须耐心等待来自卫星的修复。这可能需要几分钟,即使能够清晰地看到天空。
对于外部测试,建议您在应用中添加一个简单的文本框,将其初始化为“No GPS fix yet”,然后在该位置发送包含lat / long坐标的字符串。变化。
答案 5 :(得分:0)
也许这更清楚。如果可能的话,我会稍后将条件更改为更有效的条件
double longitude = 0;
double latitude = 0;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
} else {
Location location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
}