gpt4 book ai didi

android - 无法在 android 上使用 GPS 获取可能的位置?

转载 作者:行者123 更新时间:2023-11-29 02:09:42 25 4
gpt4 key购买 nike

这是我的代码。我尝试使用 GPS 获取我的位置,它返回纬度和经度,但我需要返回的地址列表没有返回。我该怎么办?

有没有人有最好的更新方法在 android 上使用 GPS 获取我的位置?

package com.locDetermine;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class LocDetermineActivity extends Activity implements LocationListener{
/** Called when the activity is first created. */

LocationManager locationManager = null;
Location location = null;
TextView tvLocation ;
Geocoder geo ;

//GeoPoint point ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tvLocation = (TextView) findViewById(R.id.tv_loc);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10, this);



}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

double latitude = location.getLatitude();
double longitude = location.getLongitude();

geo = new Geocoder(getApplicationContext(), Locale.getDefault());
//tvLocation.setText( "Location change : Latitude :"+latitude+"Longitude :"+longitude);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location != null) {
Toast.makeText(getApplicationContext(), "Location found", Toast.LENGTH_LONG).show();
try {
List<Address> addresses = geo.getFromLocation(latitude, longitude, 5);
String add = "";
if(addresses.size() > 0) {
for(int i =0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i);
}
tvLocation.append("\n"+add);
} catch (IOException e) {
Log.e("Where u !", "Didn't get it!",e);
}
}
else
Toast.makeText(getApplicationContext(), "Location not found", Toast.LENGTH_LONG).show();

}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}
}

最佳答案

这是我的做法:

public void getAddress(final Context context, final Handler handler) {

(geoCodThread = new Thread() {


@Override public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
Latit, Longit, 1);
//I previously defigned Latit and Longit in my location change listner
if (list != null && list.size() > 0) {
address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
//Log.e(tag, e.getMessage());
} catch (Exception e ) {
//Log.e(tag, e.getMessage());
}
finally {


Message msg = Message.obtain();
msg.setTarget(handler);



if (result != null) {
msg.what = 1;
Bundle bundle = new Bundle();
bundle.putString("address", result);
msg.setData(bundle);
msg.sendToTarget();
//Log.i(tag, "ADDRESS: "+ result);
} else {
msg.what = 0;
msg.sendToTarget();
xb.setLength(0);
xb.append("Unable to obtain address");

//Log.i(tag, "ADDRESS result is null");
}
}
}
}).start();
} // finished getting address from GPS

在我的应用程序中,我实际上只是发送了一条短信,其中显示了上面的 msg.sendToTarget(),所以我不确定整个处理程序部分是否有效,但它应该有效。

 private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
try { String result;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
result = bundle.getString("address");
break;
default:
result = null;
}
// logger3.append(result);
editor.commit();

}
finally {if (geoCodThread.isAlive())
geoCodThread.interrupt();
//finishFinding();
}
}
}

关于android - 无法在 android 上使用 GPS 获取可能的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189616/

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