Android Geocoder getFromLocationName 始终返回 null
2022-09-04 00:38:21
我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用它时会返回。我一直在尝试解决此问题数小时,但没有任何效果。这是我的代码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));
}
}