Flutter-geolocator: 当 Geolocator().placemarkFromAddress() 的地址不正确时,如何获得 null 而不是异常

创建于 2019-12-02  ·  4评论  ·  资料来源: Baseflow/flutter-geolocator

你好!

当 Geolocator().placemarkFromAddress() 的参数地址正确时,它运行良好。
但是,地址不正确(在谷歌地图上找不到),此函数返回“ PlatformException (PlatformException(ERROR_GEOCODNG_ADDRESSNOTFOUND, Unable to find坐标匹配提供的地址。, null)) ”
喜欢这张照片
2019-12-02

因此,如果地址不正确,我怎样才能获得空值而不是“PlatformException (PlatformException(ERROR_GEOCODNG_ADDRESSNOTFOUND, Unable to find坐标与提供的地址匹配。空))”的异常?

所有4条评论

你好,你解决了吗?

@NaufalHafizi你可以简单地使用 try...catch 语句:

List<Placemark> placemarks = null;
try {
  placemarks = await Geolocator().placemarkFromAddress(query);
} on PlatformException catch (e) {
  if (e.errorCode == 'ERROR_GEOCODING_ADDRESSNOTFOUND') {
    placemarks = null;
  } else {
    // Don't handle this case so rethrow the exception...
    rethrow;
  }
}

请注意,作为地理定位器 6.0.0 版的一部分,我们已将地理编码功能移至他们自己的专用插件中,并从地理定位器插件中删除了placemarkFromAddressplacemarkFromCoordinates

@NaufalHafizi你可以简单地使用 try...catch 语句:

List<Placemark> placemarks = null;
try {
  placemarks = await Geolocator().placemarkFromAddress(query);
} on PlatformException catch (e) {
  if (e.errorCode == 'ERROR_GEOCODING_ADDRESSNOTFOUND') {
    placemarks = null;
  } else {
    // Don't handle this case so rethrow the exception...
    rethrow;
  }
}

请注意,作为地理定位器 6.0.0 版的一部分,我们已将地理编码功能移至他们自己的专用插件中,并从地理定位器插件中删除了placemarkFromAddressplacemarkFromCoordinates

谢谢你

@NaufalHafizi你可以简单地使用 try...catch 语句:

List<Placemark> placemarks = null;
try {
  placemarks = await Geolocator().placemarkFromAddress(query);
} on PlatformException catch (e) {
  if (e.errorCode == 'ERROR_GEOCODING_ADDRESSNOTFOUND') {
    placemarks = null;
  } else {
    // Don't handle this case so rethrow the exception...
    rethrow;
  }
}

请注意,作为地理定位器 6.0.0 版的一部分,我们已将地理编码功能移至他们自己的专用插件中,并从地理定位器插件中删除了placemarkFromAddressplacemarkFromCoordinates

谢谢你

if (e.errorCode == 'ERROR_GEOCODING_ADDRESSNOTFOUND')
这里“e.errorcode”对我不起作用
它显示了 getter 'errorcode' 不是为 'PlatformException' 类型定义的
你会帮忙看看吗。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

domliang picture domliang  ·  7评论

hectorAguero picture hectorAguero  ·  5评论

seakmengc picture seakmengc  ·  3评论

long1eu picture long1eu  ·  4评论

samo92 picture samo92  ·  6评论