How to get coordinates of an address in android - android

How do i get gps co-ordinates of the location/address entered by user in android ?

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();
}

You can use Android's Geocoder to do reverse geocoding:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
double longitude = address.getLongitude();
double latitude = address.getLatitude();
Also include the following in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
Also note that you need to be using an API which includes a Geocoder implementation. APIs which include this are the Android Google APIs for example. You can use Geocoder.isPresent() to check if an implementation exists for your targeted API.
Check out the Geocoder documentation for more information.

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();
}
manifest permissions:-
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION

use manifest permissions like
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
and go with this
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
if(addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
}

Here is a complete sample
First define a zoom
final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
Clear any map markers
mMap.clear();
//Declare a new marker
final MarkerOptions mp = new MarkerOptions();
//declare a EditText to get the user informed address
EditText etEndereco = findViewById(R.id.map_prof_etEnd);
Geocoder geocoder = new Geocoder(map_prof.this);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
mp.position(new LatLng(latitude, longitude));
Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude);
mp.title("Selected location");
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
mMap.clear();
mMap.addMarker(mp);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
}

Related

App Crashes when entering invalid or null location using geocoder

I have been working on google maps and want the user can access location using geocoding, although it is working but the problem arises when the user enters invalid location or null location in edit text, my app gets crashed.
This is my code: (on search button click)
public void onSearch(View view) {
EditText addressSearch = (EditText) findViewById(R.id.edtSearchAddress);
String location = addressSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
addressSearch.setText("");
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
addressList= geocoder.getFromLocationName(location, 7);
} catch (IOException e) {
e.printStackTrace();
}
// location exists
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title("Find Pro"));
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
} else {
addressSearch.setText("Location does not exist");
}
}
Your kind support will be appreciated.
According to documentation the call of geocoder.getFromLocationName can throw IllegalArgumentException or return empty list. Both cases will crash your app. My bet the list is empty.
So guard your code:
if (addressList.size() > 0) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title("Find Pro"));
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
when you pass null value that time addressList return 0 value so please set IF condition before get value from addressList.
if (addressList.size() > 0) {
Address address = addressList.get(0);
}

How to get continuous Location (Lat and Lng) along with location address in same project?

I want to show a continuous update of Lat and long along with location address .
I was able to receive continuous lat/lng but wasn't able to find the location address .
Basically , want to combine these two :
https://developer.android.com/training/location/receive-location-updates.html
https://developer.android.com/training/location/display-address.html
How can I achieve this?
Thanks.
Hi you can get a location address using Geocoder class
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
String city;
try {
addresses = gcd.getFromLocation(lat,lon, 1);
if (addresses.size() > 0)
city = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}

After writing a location name,can I get my latitude and longitude in android application?

Can I write a location name in an edit text and then get it's latitude and longitude using google map in android application??If possible,then give me a way start...thanks in advance
yes you can ..code from fetching long - lat to getting address:
reverse Geocoding technique
Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}

Using Maps in Android

I have the address to certain location in terms of a string. However i want to convert this address to Lattitude and Longitude. So i used the code given below to convert into geopoint. But this geopoint is not getting added to the map. The map always shows the default map thats the entire world.
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;
} catch (IOException e) {
e.printStackTrace();
return p1;
}
}
Im new with maps and am not sure what Im doing wrong. Any suggestions?
Now that you know the latitude and longitude, you forgot to update the map to the specific location. You can do it like this:
int zoomNiveau = 15;
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), zoomNiveau));
With this code, the map will go to the location of the address. But you need to create a Marker and add it to the map if you want a Marker.
More info about the markers: https://developers.google.com/maps/documentation/android/marker

implementing the requestlocationchange in google maps

In my application, I have google maps and I'm currently showing my location using gps but i have a user input field where user can give their location, based on the user input my location should change to user location
You can use a CameraUpdate to change the position
LatLng latLng = user's position
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(latLng)
map.moveCamera(cameraUpdate);
if the user inputs an address and not the coordinates (which is very likely) you can use a geocoder to get the coordinates from the address
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
}

Categories

Resources