姬長信(Redy)

java – Android Geocoder getFromLocationName始终返…


我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用getFromLocationName()时它返回null.我一直试图解决这个问题几个小时,但没有任何工作.这是我的代码
public class MainActivity extends Activity {

    private GoogleMap mMap;

    List
addresses; MarkerOptions miami; String myLocation = "Miami,Florida"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (mMap == null) { mMap = ((MapFragment) getFragmentManager().findFragmentById( R.id.map)).getMap(); } if (mMap != null) { Geocoder geocoder = new Geocoder(this); double latitude = 0; double longitude = 0; while(addresses==null){ try { addresses = geocoder.getFromLocationName(myLocation, 1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Address address = addresses.get(0); if (addresses.size() > 0) { latitude = address.getLatitude(); longitude = address.getLongitude(); } LatLng City = new LatLng(latitude, longitude); miami = new MarkerOptions().position(City).title("Miami"); mMap.addMarker(miami); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15)); } }