How to show the progress dialog when call Geocoder methods - android

I am trying to get latitude longitude of list of address using this method
public LatLng getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(getActivity());
List<Address> address;
LatLng p1 = null;
try {
// May throw an IOException
address = coder.getFromLocationName(strAddress, 5);
if (address == null || address.size() == 0) {
return null;
}
Address location = address.get(0);
p1 = new LatLng(location.getLatitude(), location.getLongitude() );
} catch (IOException ex) {
ex.printStackTrace();
}
return p1;
}
and I called this method inside the for loop, I want to show progress dialog till all latitude longitude points are receiving but my progress dialog not showing. I assume this may because of different threads can anyone help with this issue?

Progress bar will not show up because the time taken to receive latitude and longitude takes fraction of milli seconds and will give no time to load the progress bar.

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 Latitude Longitude from more than 500 addresses?

I have more than 700 addresses in database.
I want to plot them region wise.
I have implement this code :
public LatLng getSingleLocationFromAddress(String strAddress)
{
Geocoder coder = new Geocoder(this, Locale.getDefault());
List<Address> address = null;
Address location = null;
GeoPoint p1 = null;
LatLng temp = null;//new LatLng(26.0000, 121.0000);
String strAddresNew = strAddress.replace(",", " ");
try
{
address = coder.getFromLocationName(strAddresNew, 1);
if (!address.isEmpty())
{
location = address.get(0);
location.getLatitude();
location.getLongitude();
temp = new LatLng(location.getLatitude(), location.getLongitude());
Log.d("Latlng : ", temp + "");
} else
{
arrTemp.add(strAddress);
System.out.println(arrTemp);
}
} catch (IOException e)
{
Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return temp;
}
But the problem is when I call this method in loop like this:
Marker TP1 = googleMap.addMarker(new MarkerOptions().position(getSingleLocationFromAddress(fullAddress)));
The line coder.getFromLocationName(strAddresNew, 1); returns null for some addresses, for example if I have an ArrayList of 7 addresses then I get only 4-5 LatLong values.
Use below lines of code...It can be helpful for you
Declare these variables above yours Oncreate or OncreatView
List<Address> From_geocode = null;
private double sLat, sLong;
private Geocoder geocoder;
Than inside yours oncreate or oncreateVIew use these lines of code
geocoder = new Geocoder(getActivity());
try {
From_geocode = geocoder.getFromLocationName("PUT THE ADDRESS HERE", 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (From_geocode.size() > 0) {
System.out.println("2");
From_geocode.get(0).getLatitude(); // getting
// latitude
From_geocode.get(0).getLongitude();
sLat = From_geocode.get(0).getLatitude();
sLong = From_geocode.get(0).getLongitude();
System.out.println("LATITUTE====="+sLat+" LONGITUTE====="+sLong);
}
And provide the entry inside the manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Although the google database is extensive it is possible that some of the addresses that you have supplied could not be geocoded. Hence you will not get a lat/lon for those addresses. You could probably check the status of the response.
As mentioned in the link below
https://developers.google.com/maps/documentation/geocoding/#StatusCodes
The "status" field within the Geocoding response object contains the status of the request, and may contain debugging information to help you track down why geocoding is not working. The "status" field may contain the following values:
"OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
"ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address.

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

android google map get latitude and longtitude issue

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

Android map route overlay

In my app I am using the built in MyLocationOverlay on my map.
myLocationOverlay = new MyLocationOverlay(this, mapView){
public void onLocationChanged(Location loc){
super.onLocationChanged(loc);
List<Address> address;
try{
Geocoder mGC = new Geocoder(context,Locale.ENGLISH);
address = mGC.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (address !=null){
currentAddr = address.get(0);
mHandler.post(updateUI);
}
}catch(IOException e){}
}
};
With the above code I can get the address and visually see where I am. Now i want to add a route overlay from where I am to some destination, how can I implement something like this? Is there some built in overlay for this as well?

Categories

Resources