将地理编码器与 myLocationOverlay 结合使用
我尝试从 MyLocationOverlay 提供的经度和纬度获取当前的邮政编码,并将其设置为我的 Activity 上的 EditView。
当我尝试从 MyLocationOverlay 获取经度和纬度时,活动崩溃。
这段代码有什么问题吗?
问候, 浮动
LogCat 输出: http://codepaste.net/vs6itk 第 59 行是以下行:
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.partner_suche);
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
final MapView mView = (MapView) findViewById(R.id.mapview);
mView.getController().setZoom(14);
List<Overlay> mapOverlays = mView.getOverlays();
myLocationOverlay = new MyCustomLocationOverlay(this, mView);
mapOverlays.add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Geocoder gc = new Geocoder(this, Locale.getDefault());
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
double currentLongitute = myLocationOverlay.getMyLocation().getLongitudeE6();
try
{
List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
if (addresses.size() > 0)
{
tView.setText(addresses.get(0).getLocality());
}
} catch (IOException e)
{
}
}
编辑: 我创建了一个 LocationListener 来获取我当前的位置。现在,我尝试运行 gc.getFromLocation(latitude, longitude, 1); 的地方崩溃了。我无法读取异常? :/
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,Bundle extras){ }
};
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
private void updateWithNewLocation(Location location) {
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
if (location != null) {
try
{
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
tView.setText(address.getPostalCode());
}
}
} catch (Exception e)
{
}
}
}
i try to get the current zip code from the longitude and latitude which MyLocationOverlay delivers and set this to a EditView on my Activity.
The Activity crashs when i try to get the longitude and latitude from MyLocationOverlay.
Whats wrong with this code?
Regards,
float
LogCat output: http://codepaste.net/vs6itk
Line 59 is the following line:
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
Here is my Code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.partner_suche);
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
final MapView mView = (MapView) findViewById(R.id.mapview);
mView.getController().setZoom(14);
List<Overlay> mapOverlays = mView.getOverlays();
myLocationOverlay = new MyCustomLocationOverlay(this, mView);
mapOverlays.add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Geocoder gc = new Geocoder(this, Locale.getDefault());
double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
double currentLongitute = myLocationOverlay.getMyLocation().getLongitudeE6();
try
{
List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
if (addresses.size() > 0)
{
tView.setText(addresses.get(0).getLocality());
}
} catch (IOException e)
{
}
}
EDIT:
I created a LocationListener to get my current location. Now the part crashes where i try to run gc.getFromLocation(latitude, longitude, 1); I can't read the Exception? :/
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,Bundle extras){ }
};
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
private void updateWithNewLocation(Location location) {
final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
if (location != null) {
try
{
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
{
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
tView.setText(address.getPostalCode());
}
}
} catch (Exception e)
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是模拟器 API 级别 8 或 9,并收到异常:
java.io.IOException:服务不可用
,那么这是一个已知错误,请参阅 服务不可用
但它在真实设备和模拟器级别 7 上运行正常。 (您可能也应该在地址为空的情况下设置陷阱,尽管这不会使地理编码器工作!)
If you are using an emulator API level 8 or 9, and getting the exception:
java.io.IOException: Service not Available
then it is a known bug, see service not available
It works OK on real devices and emulator level 7 though. ( You should probably put a trap on addresses being null too, though this won't make the geocoder work!)
返回的位置很可能
从or
为 NULL。可能是由于您的应用程序尚未收到位置修复,并且没有以前保存的位置。
尝试将进行地理编码的代码块移动到收到修复后运行的可运行程序中。像这样:
Most likely the location being returned from
or
is NULL. Likely due to your application not yet having received a location fix, and having no previously saved locations.
Try moving the code block where you do the geocoding into your runnable that runs after receiving a fix. Like this: