我想访问我在getMyLocationAddress中的onLocationChanged方法中的当前位置来设置文本框的文本。我想知道哪个变量是我的位置确切存储,以便我可以在geocoder.getLocationFrom()中使用它。
我在geocoder.getFromLocation(latLng.getLatitude(),latLng.getLongitude(),1)中使用LatLng对象latLng; 这是我猜的当前位置存储。但getFromLocation需要Location对象。我应该使用哪个Location对象作为LatLng对象显然会出错?
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
mCurrLocationMarker = mMap.addMarker(markerOptions);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(50));
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
public void getMyLocationAddress() {
Geocoder geocoder= new Geocoder(this, Locale.ENGLISH);
try {
//Place your latitude and longitude
//List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);
List<Address> addresses = geocoder.getFromLocation(latLng.getLatitude(),latLng.getLongitude(),1);
if(addresses != null) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
}
etOrigin.setText(strAddress.toString());
}
else
etOrigin.setText("No location found..!");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Could not get address..!", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
您需要使用位置类型的变量。
你可以在这里阅读更多内容: https://developer.android.com/reference/android/location/Location.html