I am an entry level programmer and i want to create an app, in which an address converts to a specific latitude and longtitude. Is there an easy way to do this? I read a few things about geocoder. Could you please describe to me, where and how?
This can easily be done with GeoCoder. Here is the code for rescue ;)
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((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}
where strAddress is the actual address you want long and lat.
Happy Coding :)
Related
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();
}
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
I created sample application I want to get latitude and longtitude from string address line. this is my method which I used to get latitude and longtitude
private void getFromLocation(String address) {
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(address, 1);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
latitude = p.getLatitudeE6() / 1E6;
longtitude = p.getLongitudeE6() / 1E6;
HAMBURG = new LatLng(latitude, longtitude);
Log.d("latitide", "" + latitude);
Log.d("longtitude", "" + longtitude);
} else {
Log.d("Addess", "Address not found");
}
} catch (Exception ee) {
Log.d("ex", ee.toString());
}
}
This method throws an exception " java.io.IOException: Unable to parse response from server
". Is there any mistake I made? is there any other solution to get latitude and longtitude ?
An IOException is likely to indicate that the remote request failed for some relatively low-level reason;
IOException: if the network is unavailable or any other I/O problem occurs
public List<Address> getFromLocationName (String locationName, int maxResults)
Added in API level 1
Returns an array of Addresses that are known to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", etc.. The returned addresses will be localized for the locale provided to this class's constructor.
The query will block and returned values will be obtained by means of a network lookup. The results are a best guess and are not guaranteed to be meaningful or correct. It may be useful to call this method from a thread separate from your primary UI thread.
Parameters
locationName: a user-supplied description of a location
maxResults: max number of results to return. Smaller numbers (1 to 5) are recommended
Returns:
a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
Throws
IllegalArgumentException: if locationName is null
IOException: if the network is unavailable or any other I/O problem occurs
So, Do
Please try to avoid several hits for address in a minute.
Run this code on different device.
for more detail check this artical.
List<Address> foundGeocode = null;
/* find the addresses by using getFromLocationName() method with the given address*/
try {
foundGeocode = new Geocoder(MainActivity.this).getFromLocationName("Your location",1);
Double s=foundGeocode.get(0).getLatitude(); //getting latitude
Double n=foundGeocode.get(0).getLongitude();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have two devices. One is HTC WildFire S and other one is HTC 1V. I used the Geocoder.getFromLocationName() in my application. It is running successfully in the HTC wildfire S. But in the HTC 1V i got the following error. why it's came? How can i solve this? please can anybody help me.
Code
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
//s is the address
List<Address> addresses = geoCoder.getFromLocationName(s, 5); //Here i got the following Exception.
Error
06-18 16:28:17.933: W/System.err(4960): java.io.IOException: Service not Available
06-18 16:28:17.953: W/System.err(4960):at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
Location Tab
Finally i found the answer :https://code.google.com/p/android/issues/detail?id=38009
Reboot your device for Geocoder to work. Hope it helps someone
Note: some say it will work if you use Google API 16
GeoPoint point = new GeoPoint(
(int) (LATITUDE * 1E6),
(int) (LONGITUDE * 1E6));
String n;
public void someRandomMethod() {
n = convertPointToLocation(point);
}
public String convertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
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);
}