我一直試圖對一個字符串進行地理編碼來獲取它的座標,但是我的程序總是崩潰,因爲當我嘗試使用getFromLocationName()
時它返回null
。我一直試圖解決這個問題,但沒有任何工作。這是我的代碼Android Geocoder getFromLocationName總是返回null
public class MainActivity extends Activity {
private GoogleMap mMap;
List<Address> 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));
}
}
人要回答這個問題嗎? – anishk25 2013-03-03 15:42:30