OnCreateMethod,
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
coordinates = new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()).toString();
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
and when i try to show coordinates (setText) in a TextView, it says "null"
Android Manifest,
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Even though my TextView displays "null", i get a notification on my status bar which says "Searching for GPS"
Here is my approach:
Always use a Asynch Task for getting Coordinates etc.
This is because GPS locking etc takes time and you don't want to bog down your main UI thread. Using a asynch task will enable you to throw a process in the background and auto update the UI field once the method has effectively executed.
Here is a code snippet: For the do in background part of the Asynch task
protected String doInBackground(Location... params) {
/*
* Get a new geocoding service instance, set for localized addresses.
* This example uses android.location.Geocoder, but other geocoders that
* conform to address standards can also be used.
*/
// Show user progress bar
pd.show();
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
Location location = params[0];
// Create a list to contain the result address
List<Address> addresses = null;
// Try to get an address for the current location. Catch IO or network
// problems.
try {
/*
* Call the synchronous getFromLocation() method with the latitude
* and longitude of the current location. Return at most 1 address.
*/
addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
// Catch network or other I/O problems.
} catch (IOException exception1) {
// Log an error and return an error message
Log.e(LocationUtils.APPTAG,
mContext.getString(R.string.IO_Exception_getFromLocation));
// print the stack trace
exception1.printStackTrace();
// Return an error message
return (mContext.getString(R.string.IO_Exception_getFromLocation));
// Catch incorrect latitude or longitude values
} catch (IllegalArgumentException exception2) {
// Construct a message containing the invalid arguments
String errorString = mContext.getString(
R.string.illegal_argument_exception,
location.getLatitude(), location.getLongitude());
// Log the error and print the stack trace
Log.e(LocationUtils.APPTAG, errorString);
exception2.printStackTrace();
//
return errorString;
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
// Format the first line of address
addressText = mContext.getString(
R.string.address_output_string,
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
// If there aren't any addresses, post a message
} else {
return mContext.getString(R.string.no_address_found);
}
}
After the background task has finished executing you can run a onPostExecute method.
protected void onPostExecute(String result) {
delegate.processFinished(result);
// Update the EditText on the UI Thread.
ed.setText(result);
// Hide progress bar
pd.hide();
}
In your relevant Main Class you can then call the Aysch Task and ask it to display the results.
public void getAddress(View v) {
// In Gingerbread and later, use Geocoder.isPresent() to see if a
// geocoder is available.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD
&& !Geocoder.isPresent()) {
// No geocoder is present. Issue an error message
Toast.makeText(getActivity(), R.string.no_geocoder_available,
Toast.LENGTH_LONG).show();
return;
}
if (servicesConnected()) {
// Get the current location
Location currentLocation = mLocationClient.getLastLocation();
Lat = currentLocation.getLatitude();
Lng = currentLocation.getLongitude();
LatLng = Double.toString(Lat) + "," + Double.toString(Lng);
// Message to show results
Toast.makeText(getActivity(), LatLng, 0).show();
// Turn the indefinite activity indicator on
// Start the background task
GetAddressTask getAddressTask = new GetAddressTask(getActivity(),
locationAddress, pd);
getAddressTask.delegate = this;
getAddressTask.execute(currentLocation);
} else {
Toast.makeText(getActivity(), "servicesConnected() == false",
Toast.LENGTH_SHORT).show();
}
}
Note: this is only the relevant code snippets you need. Refer to my github if you require more detailed approach. Don't forget to upvote and accept answer!
Ps:
I included a couple of pictures of an app I made to illustrate what the code can do don't worry about the map and stuff. It just shows events I have added and shared with friends who have the same app on their phone via a Google Cloud Server.
Related
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.
Below is the code I used for getting longitude and latitude within a timertask.
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
try {
gpt = new GPSTracker(MainActivity.this);
System.out.println("Internet is present");
setContentView(R.layout.tracklayout);
TimerTask myTask = new TimerTask() {
#Override
public void run() {
try {
Log.d("flow", "" + "task()");
gpt = new GPSTracker(MainActivity.this);
lc = gpt.getLocation();
if (gpt.canGetLocation()) {
double latitude = gpt.getLatitude();
double longitude = gpt.getLongitude();
Log.d("latitude", "" + latitude);
Log.d("longitude", "" + longitude);
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Reached.. ",Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
Timer myTimer = new Timer();
myTimer.schedule(myTask, 3000, 30000);
} catch (Exception e) {
e.printStackTrace();
}
} else {
alert.showAlertDialog(MainActivity.this,"Mobile Data is Off","Please Turn On mobile data to proceed", false);
}
}
});
Latitude and longitude are always getting zero even I manually entered co-ordinates through DDMS. There no problem with the code of GPSTracker. Outside timertask it worked fine. What is the problem with this code. Anyone please help
As you are using GPSTracker ,If GPS is not working than you can not have lat and long values for sure, you can't get an accurate location. It may take few minutes or seconds because it depends on lot of constraints like yours position inside building, weather , you device hardware quality etc as you are using sattelite to have locations, So we can say thatit depends on the device and the environment settings ( weather, Location under the sky/inside or outside building,device hardware quality etc as an example).
Why you are not using NETWORK_PROVIDER ?? if you can...
If you want to get a coarse location faster with program than you can get the location using NETWORK_PROVIDER, which isn't that accurate but can get you the location very faster.
Visit for examples
http://www.vogella.com/tutorials/AndroidLocationAPI/article.html
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
http://developer.android.com/guide/topics/location/strategies.html
Don't use timertask. Implement LocationListener with that activity. Write the codes in the timertask to OnLocation changed. Not needed to run in paricular time interval because we get the same data if we are in same location.
I am trying to display current loaction and longitude, latitude. I can easily fetch longitude & latitude but not able to display current location.
I am displaying my code below.
public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager ;
String provider;
Context context;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if(location!=null)
{
onLocationChanged(location);
//Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onLocationChanged(final Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);
// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );
// From here i want to get the location, as i pass current longitude &
latitude here. It passes null
in address means here it executes else instead of if.
try {
Toast.makeText(getApplicationContext(),"Hello1",Toast.LENGTH_SHORT).show();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null ) {
Address address = addresses.get(0);
// sending back first address line and locality
String result = address.getAddressLine(0) + ", " + address.getLocality();
Toast.makeText(getApplicationContext(),"result",Toast.LENGTH_SHORT).show();
}
if (addresses.size() > 0) {
String address = "";
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
Log.v("AddressTag", address);
}
else
{
Toast.makeText(getApplicationContext(),"Hello2",Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
Are you sure the Geocoder is working on your device? This is what they specify in the description of Geocoder.
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
This is only working on a few devices, if you really want to get the address you should use the google api. https://developers.google.com/maps/documentation/geocoding/
have you given these permissions in your manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
And is your device connected to internet?
check with Geocoder.isPresent() whether geocoder is present or not
If List is empty/null I bet that you do not have a Geocode backend service to do the actual lookups. The documentation says that "The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform."
If you want to use the Google Maps API for reverse lookup you need to include it in your project and obtain a Maps API key. "In previous versions of the SDK, the com.google.android.maps package was included in the standard Android library and system image. In the Android 1.5 SDK, that is not the case. The Android 1.5 library and system image do not include the Maps external library (com.google.android.maps). However, the Maps external library is available as part of the Google APIs add-on for the Android SDK"
Update: Since you mention that the GeoCoder.isPresent() returns true it means that a backend service exists and the problem lies elsewhere. I had a look at the Google Maps API FAQ and it mentions that lat/long to address lookups doesn't work everywhere. Have a look at this spreadsheet and set the Geocode filter to No. Is the country you're doing lookups for listed?
I'm trying to build a string with origin and destination gps coordinates for google mapping purposes. The first thing that I need to do is to get the gps coordinates of my current location, since this is the origin point. Then, I need to concatenate these coordinates into a larger string that I use to get directions.
I have code that gets these coordinates, and also code that concatenates them into the correct string format. However, my problem is that my string building code is running first, which is leaving me with null pointer issues since the string is referencing gps coordinates that haven't processed yet.
Here it is. The gotLocation() method comes from implementing advice in this post:
Public class DirectionsActivity extends Activity {
String myLat, myLng;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Parser parser;
LocationResult locationResult = new LocationResult(){
#Override
public void gotLocation(final Location location){
try {
Double lat = location.getLatitude();
Double lng = location.getLongitude();
if (lat != 0.0 && lng != 0.0) {
myLat = Double.toString(lat);
myLng = Double.toString(lng);
String gps_location = myLat + " " + myLng;
Toast.makeText(getBaseContext(), "First Message", Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
}
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
Toast.makeText(getBaseContext(), "Second Message", Toast.LENGTH_LONG).show();
buildString();
setContentView(R.layout.activity_directions);
}
The toast output when I run this is "Second Message" followed by "First Message". They should display in the opposite order.
Getting a location is an asynchronous operation, you don't have control on the time at which you get the answer from the system.
How would you be sure to find a satellite or cell phone mat before your toast appears ? :)
I am making an android application to show the position of a user on a map, along with the users current longitude and latitude, I have also used reverse geocoding to show the address of the android device. Now I plan to add a feature that if the user is near a particular place the phone automatically switches to silent mode, for this I have made a function to check whether the phone is already silent or not. Now I want to make another one, which puts the phone in silent mode on nearing a particular location.
The method which I have made to make the phone go silent on nearing a particular location is
public void changeToSilent(Location location){
if(distanceTo(xxxx)<100)
{
if(mPhoneIsSilent==true){
}
else
{
mPhoneIsSilent = true;
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
}
Now I am calling this method in another function, which is....
public void onLocationChanged(Location location)
{
Log.d(TAG, "onLocationChanged with location " + location.toString()); Stringtext=String.format("Lat:\t%f\nLong:\t%f\nAlt:\t%f\nBearing:\t%f",location.getLatitude(),location.getLongitude(), location.getAltitude(), location.getBearing());
this.locationText.setText(text);
changeToSilent(location);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point);
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
Now I am calling the above function in the oncreate method. Now suppose I know the latitude and longitude of the place near which the phone should go to silent.
What I want to know is, what do I have to write in place of xxxx in the changetosilent method?
You can use the AudioManager to change the system volume levels.
here is an example:
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
//am.setStreamVolume(AudioManager.STREAM_MUSIC,6,0); //<-- use this one to set the volume
am.setRingerMode(AudioManager.RINGER_MODE_SILENT); //<-- to put on mute.
EDIT: Get a Location object and use loc.distanceBetween();