日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android 获取粗略位置_在Android上获取用户当前位置的最简单,最强大的方法是什么?...

發布時間:2025/3/20 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 获取粗略位置_在Android上获取用户当前位置的最简单,最强大的方法是什么?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在搜索最佳實現后,如何獲得最佳的精確用戶位置,我設法結合所有最好的方法,并提出以下類:/**

*?Retrieve?accurate?location?from?GPS?or?network?services.

*

*

*?Class?usage?example:

*

*?public?void?onCreate(Bundle?savedInstanceState)?{

*??????...

*??????my_location?=?new?MyLocation();

*??????my_location.init(main.this,?locationResult);

*?}

*

*

*?public?LocationResult?locationResult?=?new?LocationResult(){

*??????@Override

*??????public?void?gotLocation(final?Location?location){

*??????????//?do?something

*??????????location.getLongitude();

*??????????location.getLatitude();

*??????}

*??};

*/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?

//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);

Iterablesatellites?=?status.getSatellites();

sat_count?=?0;

IteratorsatI?=?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;

}

}}

min_gps_sat_count如果啟用GPS,此課程將嘗試連接到衛星。否則返回LocationManager.getBestProvider()位置。檢查代碼!

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的android 获取粗略位置_在Android上获取用户当前位置的最简单,最强大的方法是什么?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。