0

我正在尝试显示当前位置和经度、纬度。我可以轻松获取经度和纬度,但无法显示当前位置。我在下面显示我的代码。

public class MainActivity extends Activity implements LocationListener{ 
LocationManager locationManager ;
     String provider;
     Context context;

     @Override public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
     }
}




    if(provider!=null && !provider.equals("")){

        // Get the location from the given provider 
        Location location = locationManager.getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 20000, 1, this);


        if(location!=null)
        {
            onLocationChanged(location);


             //Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();



        }
        else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

    }else{
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(final Location location) {
    // Getting reference to TextView tv_longitude
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

    // Getting reference to TextView tv_latitude
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

    // Setting Current Longitude
    tvLongitude.setText("Longitude:" + location.getLongitude());

    // Setting Current Latitude
    tvLatitude.setText("Latitude:" + location.getLatitude() );


// From here i want to get the location, as i pass current longitude &

这里的纬度。它在地址中传递 null 意味着它在这里执行 else 而不是 if。

    try {
         Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

         if (addresses != null ) {
             Address address = addresses.get(0);
             // sending back first address line and locality
            String result = address.getAddressLine(0) + ", " + address.getLocality();
            Toast.makeText(getApplicationContext(),"result",Toast.LENGTH_SHORT).show();
         }
    if (addresses.size() > 0) {
        String address = "";
        for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
            address += addresses.get(0).getAddressLine(index) + " ";

        Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
        Log.v("AddressTag", address);
    }

    else
    {
         Toast.makeText(getApplicationContext(),"Hello2",Toast.LENGTH_SHORT).show();
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   

}




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

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

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}
4

3 回答 3

0

你确定Geocoder在你的设备上工作吗?这是他们在Geocoder的描述中指定的。 The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists. 这仅适用于少数设备,如果您真的想获取地址,您应该使用 google api。https://developers.google.com/maps/documentation/geocoding/

于 2013-05-17T06:47:50.573 回答
0

您是否在清单中授予了这些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

您的设备是否连接到互联网?

使用 Geocoder.isPresent() 检查地理编码器是否存在

于 2013-05-17T10:08:57.397 回答
0

如果 List 为空/null,我敢打赌您没有 Geocode 后端服务来进行实际查找。文档说“ Geocoder 类需要一个不包含在核心 android 框架中的后端服务。如果平台中没有后端服务,Geocoder 查询方法将返回一个空列表。”

如果您想使用Google Maps API进行反向查找,您需要将其包含在您的项目中获取 Maps API 密钥。“在之前版本的SDK中,com.google.android.maps包被包含在标准的Android库和系统镜像中。在Android 1.5 SDK中,情况并非如此。Android 1.5库和系统镜像不包含地图外部库 (com.google.android.maps)。但是,地图外部库可作为 Android SDK 的 Google API 插件的一部分使用”

更新:由于您提到 GeoCoder.isPresent() 返回 true,这意味着存在后端服务并且问题出在其他地方。我查看了 Google Maps API 常见问题解答,其中提到地址查找的纬度/经度并不适用于所有地方。查看此电子表格并将地理编码过滤器设置为否。您要查找的国家/地区是否已列出?

于 2013-05-17T06:44:20.803 回答