如何从谷歌地图上的移动地图获取当前地址 [英] How to get current address from google map on moving map
问题描述
我想在地图移动或拖动当前位置的地址,在地图上移动标记应该改变该位置,显示此像下面的文本视图:的
I want to get current location address when the map is moved or dragged , when the map is moved the marker should change on that location and show this on text view like below:
如果我正确的做用反地理code位置。我有一个教程尝试这样做code从这里:<一href=\"http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/\" rel=\"nofollow\">http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/
If I am right its done using reverse geocode Location. I have tried this code by one tutorial from here :http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/
不过我得到的经纬度不是地址。而且我不能够移动地图和更改标记位置我怎么可以这样?
Still I am getting the latitude and longitude not the address. And I am not able to move the map and change the marker location how can I do this???
ChooseFromMapActivity
ChooseFromMapActivity
public class ChooseFromMapActivity extends AppCompatActivity{
AppLocationService appLocationService;
LinearLayout UseLocation;
TextView textShowAddress;
private GoogleMap mMap;
MarkerOptions markerOptions;
double latitude;
double longitude;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_from_map);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
}
UseLocation =(LinearLayout)findViewById(R.id.LinearLayoutUseLoc);
textShowAddress =(TextView)findViewById(R.id.textShowAddress);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
// Getting reference to the Google Map
mMap = mapFragment.getMap();
mMap.setMyLocationEnabled(true);
latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
appLocationService = new AppLocationService(
ChooseFromMapActivity.this);
UseLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Location location = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude, longitude,
getApplicationContext(), new GeocoderHandler());
} else {
showSettingsAlert();
}
}
});
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ChooseFromMapActivity.this);
alertDialog.setTitle("SETTINGS");
alertDialog.setMessage("Enable Location Provider! Go to settings menu?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
ChooseFromMapActivity.this.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String locationAddress;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
break;
default:
locationAddress = null;
}
textShowAddress.setText(locationAddress);
}
}
}
AppLocationService
AppLocationService
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
LocationAddress
LocationAddress
public class LocationAddress {
private static final String TAG = "LocationAddress";
public static void getAddressFromLocation(final double latitude, final double longitude,
final Context context, final Handler handler) {
Thread thread = new Thread() {
@Override
public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> addressList = geocoder.getFromLocation(
latitude, longitude, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
result = sb.toString();
}
} catch (IOException e) {
Log.e(TAG, "Unable connect to Geocoder", e);
} finally {
Message message = Message.obtain();
message.setTarget(handler);
if (result != null) {
message.what = 1;
Bundle bundle = new Bundle();
result = "Latitude: " + latitude + " Longitude: " + longitude +
"\n\nAddress:\n" + result;
bundle.putString("address", result);
message.setData(bundle);
} else {
message.what = 1;
Bundle bundle = new Bundle();
result = "Latitude: " + latitude + " Longitude: " + longitude +
"\n Unable to get address for this lat-long.";
bundle.putString("address", result);
message.setData(bundle);
}
message.sendToTarget();
}
}
};
thread.start();
}
}
我
如何才能得到结果如图形象??
How can I get the result as shown in image??
推荐答案
试试这个,它在你的地理coderHandler类微小的变化,
Try this and it's a minor change in your GeocoderHandler class,
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case 1:
Bundle bundle = message.getData();
strAddress = bundle.getString("address");
break;
default:
strAddress = null;
}
Log.e("LAT", "Latitude : " + valueLatitude);
Log.e("LONG", "Longitude : " + valueLongitude);
Log.e("ADDRESS", "Address : " + strAddress);
LatLng latLng = new LatLng(valueLatitude, valueLongitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(
latLng, 15);
googleMap.animateCamera(yourLocation);
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Current Location")
.snippet(strAddress)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pin)));
}
}
这篇关于如何从谷歌地图上的移动地图获取当前地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!