試試這個:
將這個全球:
private LocationManager location_manager;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location = null; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
從電話,無論你想這個方法:
public Location getCurrentLatLong(Context conext,LocationListener con)
{
try
{
location_manager = (LocationManager) ((Activity) con)
.getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = location_manager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = location_manager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
Toast.makeText(conext, "No provider is enabled", Toast.LENGTH_SHORT).show();
}
else
{
this.canGetLocation = true;
if (isNetworkEnabled)
{
location_manager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, con);
Log.d("Network", "Network Enabled");
if (location_manager != null)
{
location = location_manager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
if (location == null)
{
location_manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, con);
Log.d("GPS", "GPS Enabled");
if (location_manager != null)
{
location = location_manager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return location;
}
使用GPS找到位置需要更多的時間,所以你收到空,如果你使用網絡提供商,你得到它在第二 – appukrb
打開本地地圖的應用程序,然後打開你的應用程序,它給出的位置,這是GPS提供商的問題,更好,你可以嘗試熔合位置提供商,以避免這個問題 –