Getting LatLng from adress - android

I am developing an android application and in that application, I want to search for places on a map without using places.api. is there a way to get LatLng from a given address?

You can get address from Latng by using Geocoder,please check below code
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 G`enter code here`eoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}

Related

Finding the latitude and longitude from an 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());
}

When find the latitude and longitude from address, how to give the address?

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

Can not resolve symbol GeoPoint in android studio

I am using android studio 0.8.9. i am using google map v2 and google services 5.2.08.i want to display marker from given address and also generated SHA Key using key-tool also enabled and get my API Key from google console. But when i am using GeoPoint for getting Lat and Long android studio does not recognize GeoPoint class.
My Java Activity code:
public class WelcomeUser extends ActionBarActivity {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_user);
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if(null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
double lat= 0.0, lng= 0.0;
String address = "Ahmedabad, Gujarat";
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));
lat=p.getLatitudeE6()/1E6;
lng=p.getLongitudeE6()/1E6;
Log.d("Latitude", ""+lat);
Log.d("Longitude", ""+lng);
}
}
catch(Exception e)
{
e.printStackTrace();
}
if(null != googleMap){
googleMap.addMarker(new MarkerOptions()
.position(p)
.title("Marker")
.draggable(true)
);
}
}
Please help me guys.Thanks in advance
Geocoder no longer provided latitude-longitude from addresses,use Reverse Geocoding API to get latitude-longitude from address.
Check Example here : Get latitude,longitude from address in Android

Distance between user's location and a location entered by user

I want to find the distance between the location of the user and a place he enters. The entered location will be an address. Using location class I can find the coordinates of the user's location but how can I find those of the entered location. Couldn't find that in Google Maps API as well.
You can retrieve the location from an address like this:
public GeoPoint getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address = new ArrayList<Address>();
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
GeoPoint p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
} catch (Exception e) {}
return null;
}
And for the distance use distanceBetween.

How to Search Address by Name on Google Map Android

I am a beginner of Android Developer. I am developing the Map Application. I have a function of searching address but I do not know how to search address by name using Google Map API V.2. Please suggest me the solution and some code to solve this. Thank you very much and Sorry with my English.
adderess = c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
// get address in string for used location for the map
/* get latitude and longitude from the adderress */
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(adderess, 5);
if (addresses.size() > 0)
{
Double lat = (double) (addresses.get(0).getLatitude());
Double lon = (double) (addresses.get(0).getLongitude());
Log.d("lat-long", "" + lat + "......." + lon);
final LatLng user = new LatLng(lat, lon);
/*used marker for show the location */
Marker hamburg = map.addMarker(new MarkerOptions()
.position(user)
.title(adderess)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
catch (IOException e)
{
e.printStackTrace();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_map);
editText = (EditText) findViewById(R.id.editText1);
mapView = (MapView) findViewById(R.id.mapView);
btngo = (Button) findViewById(R.id.btnmapsites);
btngo.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
List<Address> addresses;
try{
addresses = geoCoder.getFromLocationName(editText.getText().toString(),1);
if(addresses.size() > 0){
p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
editText.setText("");
}else{
AlertDialog.Builder adb = new AlertDialog.Builder(SearchMapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("Please Provide the Proper Place");
adb.setPositiveButton("Close",null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}

Categories

Resources