Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。相关的知识,希望对你有一定的参考价值。

1、首先,检查那个供应商是有效的。有些在设备上可能没效,有些可能在应用程序清单中无效。
2、如果有有效的供应商,启动位置监听和路由失效定时器。
3、如果从位置监听器获得更新,用供应的值,停止监听器和定时器。
4、如果没有获得更新,并且时间已经结束,那就用最后一个已知值。
5、从有效供应商获取最后一个已知值并且选择最接近的。
class MyLocation

/**
* If GPS is enabled.
* Use minimal connected satellites count.
*/
private static final int min_gps_sat_count = 5;

/**
* Iteration step time.
*/
private static final int iteration_timeout_step = 500;

LocationResult locationResult;
private Location bestLocation = null;
private Handler handler = new Handler();
private LocationManager myLocationManager;
public Context context;

private boolean gps_enabled = false;

private int counts = 0;
private int sat_count = 0;

private Runnable showTime = new Runnable()

public void run()
boolean stop = false;
counts++;
System.println("counts=" + counts);

//if timeout (1 min) exceeded, stop tying
if(counts > 120)
stop = true;


//update last best location
bestLocation = getLocation(context);

//if location is not ready or don`t exists, try again
if(bestLocation == null && gps_enabled)
System.println("BestLocation not ready, continue to wait");
handler.postDelayed(this, iteration_timeout_step);
else
//if best location is known, calculate if we need to continue to look for better location
//if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec)
if(stop == false && !needToStop())
System.println("Connected " + sat_count + " sattelites. continue waiting..");
handler.postDelayed(this, iteration_timeout_step);
else
System.println("#########################################");
System.println("BestLocation finded return result to main. sat_count=" + sat_count);
System.println("#########################################");

// removing all updates and listeners
myLocationManager.removeUpdates(gpsLocationListener);
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeGpsStatusListener(gpsStatusListener);
sat_count = 0;

// send best location to locationResult
locationResult.gotLocation(bestLocation);



;

/**
* Determine if continue to try to find best location
*/
private Boolean needToStop()

if(!gps_enabled)
return true;

else if(counts <= 4)
return false;

if(sat_count < min_gps_sat_count)
//if 20-25 sec and 3 satellites found then stop
if(counts >= 40 && sat_count >= 3)
return true;

return false;


return true;


/**
* Best location abstract result class
*/
public static abstract class LocationResult
public abstract void gotLocation(Location location);


/**
* Initialize starting values and starting best location listeners
*
* @param Context ctx
* @param LocationResult result
*/
public void init(Context ctx, LocationResult result)
context = ctx;
locationResult = result;

myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

bestLocation = null;
counts = 0;

// turning on location updates
myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener);
myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);
myLocationManager.addGpsStatusListener(gpsStatusListener);

// starting best location finder loop
handler.postDelayed(showTime, iteration_timeout_step);


/**
* GpsStatus listener. OnChainged counts connected satellites count.
*/
public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener()
public void onGpsStatusChanged(int event)

if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS)
try
// Check number of satellites in list to determine fix state
GpsStatus status = myLocationManager.getGpsStatus(null);
Iterable<GpsSatellite>satellites = status.getSatellites();

sat_count = 0;

Iterator<GpsSatellite>satI = satellites.iterator();
while(satI.hasNext())
GpsSatellite satellite = satI.next();
System.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation());
sat_count++;

catch (Exception e)
e.printStackTrace();
sat_count = min_gps_sat_count + 1;


System.println("#### sat_count = " + sat_count);


;

/**
* Gps location listener.
*/
public final LocationListener gpsLocationListener = new LocationListener()
@Override
public void onLocationChanged(Location location)


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

/**
* Network location listener.
*/
public final LocationListener networkLocationListener = new LocationListener()
@Override
public void onLocationChanged(Location location)


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

/**
* Returns best location using LocationManager.getBestProvider()
*
* @param context
* @return Location|null
*/
public static Location getLocation(Context context)
System.println("getLocation()");

// fetch last known location and update it
try
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
String strLocationProvider = lm.getBestProvider(criteria, true);

System.println("strLocationProvider=" + strLocationProvider);
Location location = lm.getLastKnownLocation(strLocationProvider);
if(location != null)
return location;

return null;
catch (Exception e)
e.printStackTrace();
return null;


参考技术A 可以做到直接call到GPS模块,读取当前的经纬度。
但经纬度->实际地址的转换,google提供了api供程序调用,需要去申请一个google maps api的key,然后发送经纬度,会返回实际地址。
参考技术B 你百度查查有没有不需要地图就显示地理位置的 可能没有 ip地址大多是动态的 参考技术C QQ空间等也可以大概知道位置,发表说说的位置

以上是关于Android怎么实现点击按钮获取当前位置的信息,没有地图的情况下。的主要内容,如果未能解决你的问题,请参考以下文章

Android 使用GPS获取地理位置

工作日报 2022.1.17 Android 获取系统的震动功能

Android 怎么使用 最简单 的方法来获取手机当前的位置(获取手机当前的经,纬度),求详细

怎么用vue实现点击图片(按钮)的波纹效果(涟漪动效)

Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

页面在获取当前位置时未加载