java - 地理编码器无法通过 getfromlocationName 工作

标签 java google-geocoder

我的代码如下:

Geocoder geocoder = new Geocoder(this,Locale.UK );
         List<Address> addresses = null;
         Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 
   try {
    Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 

    addresses = geocoder.getFromLocationName( 
      "Camden", 1);

    if (addresses.size() > 0) {

           geopoint = new GeoPoint(
                         (int) (addresses.get(0).getLatitude() * 1E6), 
                         (int) (addresses.get(0).getLongitude() * 1E6));

           //Animating on display

           mapcontroller.animateTo(geopoint);
           mapcontroller.setZoom(5); 
           mapview.invalidate();
          } 


   } catch (IOException io) {
    // TODO Auto-generated catch block
    Toast.makeText(displaybyName.this, "Connection Error", Toast.LENGTH_SHORT).show();
   }

   if (addresses.size() > 0) {

          geopoint = new GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

          //Animating on display

          mapcontroller.animateTo(geopoint);
          mapcontroller.setZoom(5); 
          mapview.invalidate();
         } 

但是,应用程序每次都会崩溃。地理编码器不返回任何纬度/经度。

代码有什么问题吗...请指教。

最佳答案

仔细检查

addresses = geocoder.getFromLocationName("Camden", 1);

返回非 null 的内容 - 否则:以下行将引发 NullPointerException

<小时/>

进一步阅读

关于java - 地理编码器无法通过 getfromlocationName 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4780383/

相关文章:

parsing - 理解 Google Geocoding API 结果

javascript - GoogleMaps,反向地理编码,返回 street_address

java - 我应该使用多个 Lucene 目录/索引来搜索不同类型的数据吗?

java - 多线程写入多个文件,但是会互相影响

java - 在 Eclipse 中的 "Run configurations"向导的目标中添加注释。

java - 单击按钮时是否可以调用不同的类

java - 使用 Avro 文件分页

javascript - 从谷歌地理编码API获取latLong

java - Geocoder 并不总是返回一个值

javascript - 地理编码服务与 gMap 搜索之间的差异结果