How to zoom to a country by name in an Android application(google map)? IF I get a coutry name Ireland then the map location to the Ireland
1 Get location by country name
Geocoder geocoder = new Geocoder(<your context>);
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
2 Zoom to current location
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
You can use the Geocoder API to fetch the location based on the country name and then animate the map camera to that position:
try {
List<Address> address = new Geocoder(this).getFromLocationName("Ireland", 1);
if (address == null) {
Log.e(TAG, "Not found");
} else {
Address loc = address.get(0);
Log.e(TAG, loc.getLatitude() + " " + loc.getLongitude());
LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude())
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 14));
}
} catch (IOException e) {
e.printStackTrace();
}
Related
I am getting current latitude and longitude in map box, I need current timezone based on current latitude and longitude.
For example.My device time is the USA but my location showing in India so I need India timezone.Can you please help me.
#Override
public void onLocationChanged(Location location) {
try {
mLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Log.i("EE","mLatLngmLatLng"+mLatLng);
String time = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS").format(location.getTime());
Log.i("EE","time----"+time);
Log.i("EE","vlocation.getTime()----"+location.getTime());
updateCamera();
if (mMarker != null) {
updateMarker();
}else{
addMarker();
}
} catch (Exception e) {
e.printStackTrace();
}
Get address by Geocoder and identify country, When you will get the country name It will be easy for you to identify the time Zone.
private void getAddressFromLocation(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address fetchedAddress = addresses.get(0);
StringBuilder strAddress = new StringBuilder();
for (int i = 0; i < fetchedAddress.getMaxAddressLineIndex(); i++) {
strAddress.append(fetchedAddress.getAddressLine(i)).append(" ");
}
txtLocationAddress.setText(strAddress.toString());
} else {
txtLocationAddress.setText("Searching Current Address");
}
} catch (IOException e) {
e.printStackTrace();
printToast("Could not get address..!");
}
}
I am trying to display markers on map in android by using address.
This is my code:
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
Geocoder coder= new Geocoder(this);
try
{
String straddress = "155 Park Theater,Palo Alto,CA";
Double latitude = 0.0;
Double longitude = 0.0;
List<Address> addresses = coder.getFromLocationName("155 Park Theater,Palo Alto,CA",1);
Address location = addresses.get(0);
LatLng p1 = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(p1).title("California"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(p1));
}
catch (Exception e)
{
e.printStackTrace();
}
}
If I do not put street number, then it will display the marker.
But, it will just display marker on the street. I want marker on particular location.
I am getting following error while inputting the whole address with street number. The "address" list is empty.
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Update your code with following :
List<Address> address;
LatLng latLng = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
latLng = new LatLng(location.getLatitude(),location.getLongitude());
}
I follow this question (How can I find the latitude and longitude from address?) and try to get the latitude and longitude of an address. I call the method like this "GeoPoint point=getLocationFromAddress("colombo,sri lanka");" . but give null value. how to call method correctly??
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}
}
In my opinion you should use google place autocomplete/place picker api's.
Select the place and thereafter you can get Latitude and Longitude.
Check this out:
https://developers.google.com/places/android-api/autocomplete
try below code
Geocoder gcd = new Geocoder(ReserveTimer.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(Double.parseDouble(lati), Double.parseDouble(longi), 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getAddressLine(0));
System.out.println(addresses.get(0).getAdminArea());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getPostalCode());
}
You can use the following method that take a Location object that contain latitud and longitud coordinates and Log the location to the LogCat console:
public void setLocation(Location loc) {
//Get Human readable address from latitud and longitud coordinates
if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (!list.isEmpty()) {
Address address = list.get(0);
Log.d(CLASS_TAG, "Mi direcci—n es: \n" + address.getAddressLine(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want to do reverse geocoding in my app using map api 2.But i dont know exactly how to do that?Any ideas?
Use Geocoder:
Geocoder geoCoder = new Geocoder(context);
List<Address> matches = geoCoder.getFromLocation(latitude, longitude, 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
This is how it works for me..
MarkerOptions markerOptions;
Location myLocation;
Button btLocInfo;
String selectedLocAddress;
private GoogleMap myMap;
LatLng latLng;
LatLng tmpLatLng;
#Override
public void onMapLongClick(LatLng point) {
// Getting the Latitude and Longitude of the touched location
latLng = point;
// Clears the previously touched position
myMap.clear();
// Animating to the touched position
myMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
//tmpLatLng = latLng;
btLocInfo.setEnabled(true);
btLocInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] coordinates={tmpLatLng.latitude/1E6,tmpLatLng.longitude/1E6};
double latitude = tmpLatLng.latitude;
double longitude = tmpLatLng.longitude;
Log.i("selectedCoordinates", latitude + " " + longitude);
Log.i("selectedLocAddress", selectedLocAddress);
}
});
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
// Finding address using reverse geocoding
#Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
Thread.sleep(500);
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
selectedLocAddress = addressText;
return addressText;
}
#Override
protected void onPostExecute(String addressText) {
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(addressText);
// Placing a marker on the touched position
myMap.addMarker(markerOptions);
}
}
You can do like this to get complete address :
public class MainActivity extends AppCompatActivity {
...
private Geocoder geocoder;
private TextView mAddressTxtVu;
...
// assume that you got latitude and longitude correctly
mLatitude = 20.23232
mLongitude = 32.999
String errorMessage = "";
geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
mlattitude,
mlongitude,
1);
} catch (IOException e) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, e);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +",
Longitude = " + mlongitude, illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
// Log.i(TAG, getString(R.string.address_found));
mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
Hope it helps!
You don't need to use Google Maps Api for this purpose. Android SKD have a class for it which you can simply use without any registration of API Key and so on. The class is android.location.Geocoder. It have methods for geocoding and reverse geocoding. I was looking in the source code of this class and found that it have a method android.location.Geocoder#getFromLocationName(java.lang.String, int) where first argument is address, and second is max number of results you want. It returns a List<Address>. The Address class have methods like android.location.Address#getLatitude and android.location.Address#getLongitude. They both return double.
Try it and let me know how good it is :-)
I have an application that will get the latitude and the longitude of the users location. What I need to know is how do I take the latitude and the longitude of the location and convert it into just the address of the user?
Thanks for any help.
You can use GeoCoding. Follow the instructions on this link where it says Geocoding and Reverse Geocoding
You can find code there that you can try it out on your own app (copy/paste)
I hope it helps :)
Try using Geocoder
try {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(MainActivity.this);
Toast.makeText(this, "before if",
Toast.LENGTH_LONG).show();
if (latitude != 0 || longitude != 0) {
addresses = geocoder.getFromLocation(latitude ,
longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Log.d("TAG", "address = "+address+", city ="+city+", country = "+country );
Toast.makeText(this, "address = "+address+", city ="+city+", country = "+country, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "latitude and longitude are null",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}