gpt4 book ai didi

android - 在android中立即获取位置

转载 作者:行者123 更新时间:2023-11-29 18:20:04 27 4
gpt4 key购买 nike

我想点击一个按钮并接收当前位置,我知道我无法立即获取位置,所以这就是我所做的:点击事件:

        public void onClick(View v)
{
ProgressDialog MyDialog = ProgressDialog.show( MainPage.this, " " , " Loading. Please wait ... ", true);
MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this);
myActionsHandler.startSearch();
MyDialog.dismiss();
Intent intent = new Intent(MainPage.this, ResultPage.class);
startActivity(intent);
}

这是搜索位置的处理程序

    public void startSearch(long timeInterval,float distanceInterval)
{
LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval,
distanceInterval, this);

while(!_locationFound)
{
//wait till location is found
}
}

public void onLocationChanged(Location location)
{
if (location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();
float bearing = location.getBearing();

Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
try
{
doTheProcess(_searchType,latitude, longitude, speed, bearing);
_locationFound = true;
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

我知道这是行不通的,因为循环在同一个线程中,那么您建议最好的解决方案是什么?

在requestLocationUpdates的javadoc中,有“调用线程必须是Looper线程,比如调用Activity的主线程”。但我还没有找到任何例子,所以我不知道这是否是正确的解决方案。

还有一个问题,即使我以前从未调用过 locationManager,getLastKnownLocation() 是否也能正常工作?谢谢

最佳答案

我以前遇到过同样的问题..但我找到了解决方案..这是即时获取位置的最简单方法。

public class LocationFinder extends Activity {

TextView textView1;
Location currentLocation;
double currentLatitude,currentLongitude;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textView1 = (TextView) findViewById(R.id.textView1);
Log.i("@@@@@@@@@@ Inside LocationFinder onCreate", "LocationFinder onCreate");

FindLocation();

}

public void FindLocation() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateLocation(location);

Toast.makeText(
LocationFinder.this,
String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude), 5000)
.show();

}

public void onStatusChanged(String provider, int status,
Bundle extras) {
}

public void onProviderEnabled(String provider) {
}

public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

}


void updateLocation(Location location) {
currentLocation = location;
currentLatitude = currentLocation.getLatitude();
currentLongitude = currentLocation.getLongitude();
textView1.setText(String.valueOf(currentLatitude) + "\n"
+ String.valueOf(currentLongitude));

}
}

关于android - 在android中立即获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6002630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com