I am doing a GPS app, with latitude, longitude, speed, altitude...but I don't get any data for this one. I always get 1.0 as a value. (With the speed it happens the same).
I tried with the simulator of GPX in Eclipse (DDMS) and on the terminal (KML simulator, not on the street)...but it always shows 0.0 or 1.0.
My code is the following:
private Location currentLoc;
private Location previousLoc;
The criteria for the GPS...
// Criteria object is optional. Will return value representing "GPS"
Criteria oGPSSettings = new Criteria();
oGPSSettings.setAccuracy(Criteria.ACCURACY_FINE);
oGPSSettings.setSpeedRequired(true);
oGPSSettings.setAltitudeRequired(true);
oGPSSettings.setBearingRequired(false);
oGPSSettings.setCostAllowed(false);
oGPSSettings.setPowerRequirement(Criteria.POWER_MEDIUM);
When a new location is recieved...
#Override
public void onLocationChanged(Location location) {
currentLoc = location;
String sText = "Altitude: " + currentLoc.getAltitude();
txtOutput.setText(sText);
}
// sets this location as last location.
previousLoc = currentLoc;
}
I removed from the code things like displaying latitude, longitude...right now I'll show only altitude.
The question is: Why do you think is it happening this? The latitude and longitude works fine, but not the altitude. Maybe is it a hardware issue? Because the code seems correct, I think. Only with the getAltitude function should works.
Thank you in advance!
Did you ask for "android.permission.ACCESS_FINE_LOCATION" in your manifest?
also try changing ACCURACY_FINE to ACCURACY_HIGH
Related
I am trying to do something along the lines of signal strength triangulation using cell towers, on android. I'm using android studio and java. I've found useful csv with cell tower latitude and longitude, and written some fairly trivial code to parse it, all I need now is the cell towers' mcc, mnc, lac and cell id, so I can search the csv and find the lat and long. I'm using the telephonyManager class and .getAllCellInfo(), like so:
tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
InputStream inputStream = getResources().openRawResource(R.raw.cells);
CsvFile csvFile = new CsvFile(inputStream);
List<String[]> res = csvFile.read();
List<CellInfo> cellsTemp = tel.getAllCellInfo();
for (CellInfo ci : cellsTemp) {
if (ci instanceof CellInfoLte) {
Log.d("Cells: ", ((CellInfoLte) ci).getCellIdentity().toString());
this.cellsLte.add((CellInfoLte) ci);
this.towers.add(new cellTower((CellInfoLte) ci, res));
}
}
However, when I log the cellIdentity of those cells, I get this:
CellIdentityLte:{ mCi=2147483647 mPci=313 mTac=2147483647 mEarfcn=1300 mBandwidth=2147483647 mMcc=null mMnc=null mAlphaLong= mAlphaShort=}
As you can see, the mcc and mnc are null, and the the cell id and location code are 2147483647, or Integer.MAX_VALUE, which as I understand it, is used when they are for whatever reason unavailable.
I have the ACCESS_FINE_LOCATION and READ_PHONE_STATE runtime permissions, and I've added them to the manifest file as well. I've also tried just logging the objects directly from tel.getAllCellInfo(), same exact result.
This is the declaration for tel and towers:
public class MainActivity extends AppCompatActivity {
protected RequestQueue requestQueue;
protected TelephonyManager tel;
protected List<CellInfoLte> cellsLte = new ArrayList<CellInfoLte>();
protected List<cellTower> towers = new ArrayList<cellTower>();
And cellTower is just a sort of wrapper class that contains some information I'm going to need to calculate distance later. It also contains the cellInfo of the tower.
I am also aware that there are apis that can do this whole thing automatically, but I need to do this myself, as a sort of a proof of concept.
I'm running this on a LGE LG-H870, but I've also tried it on a Xiaomi Redmi 8, and had the same issue.
I am an (almost)complete beginner with android studio, but I think I have a perfectly decent understanding of java. Is there any way at all to get around this? Any help would be greatly appreciated.
I'm trying to calculate the distance between my Geolocation point and a marker tracing a polyline between these two, but the problem is when i output the result and it gives me an enourmous distance(1.125405E7) instead of (For Example) 1,250m.
This is my code
//Gets the latitude & longitude of my Geolocation
Location location = new Location("A");
location.setLatitude(location.getLatitude());
location.setLongitude(location.getLongitude());
//Gets the location of a marker/Polyline which i put using onMapLongCliclistener
Location locationb = new Location("B");
locationb.setLatitude(latLng.latitude);
locationb.setLongitude(latLng.longitude);
//Calculates the distance between Geolocation/Marker
double distance=locationb.distanceTo(location);
String distancia = String.valueOf(distance);
Toast.makeText(MapsActivity.this,distancia,
Toast.LENGTH_SHORT).show();
Any tip or solution?
Thank You Very Much!
From you code line no 3 'location.setLongitude(location.getLatitude());' is wrong. You are setting longitude instead of latitude.
Code:
double distance = SphericalUtil.computeDistanceBetween(new LatLng(location.getLatitude(),location.getLongitude()),new
LatLng(latLng.latitude,latLng.longitude));
String distanceC = String.valueOf(distance);
txtDistancia.setText(new DecimalFormat("##.##").format(distanceC)+" "+"mts");
I'm trying to create an adndroid application which will shows a distance from moment when I started the app.
I'm using Location Manager and this is my idea:
Check coordinates.
(2 sec delay)
Chceck coordinates2
If coordinates2 != coordinates then calculate distance between them and add it to double distance.
Repeat
public void onLocationChanged(final Location location) {
sz = location.getLatitude();
dl = location.getLongitude();
String dls = String.valueOf(dl);
String szs = String.valueOf(sz);
aktualna_dlg.setText(dls);
aktualna_szg.setText(szs);
lat1 = location.getLatitude();
lon1 = location.getLongitude();
int secs = 2; // Delay in seconds
Utils.delay(secs, new Utils.DelayCallback() {
#Override
public void afterDelay() {
lat2 = location.getLatitude();
lon2 = location.getLongitude();
}
});
if (lat1!=lat2 || lon1!=lon2){
distance += DistanceCalculator.distance(lat1,lon1,lat2,lon2, "K");
}
if(distance<1000){
String distance_s = String.valueOf(distance*1000);
distance_tv.setText(distance_s + " m");
}
else if(distance>=1000){
String distance_s = String.valueOf(distance/1000);
distance_tv.setText(distance_s + " km");
}
}
But when I compile the app and catch GPS, I'm getting a distance ~ 6km.
What am I doing wrong?
I'm not sure what utils.delay does, since it isn't part of the framework. But assuming it works correctly- it happens on a delay. That means lat2 and lon2 won't update until its called. All of the code that uses those variables has to happen in afterDelay, they aren't valid until then.
Eliminate the delays and simply compare the argument to onLocationChanged with the argument from the last time it was called. As for why you're recording a huge distance traveled...
Use a high-pass filter
How far do you expect to move in 2 seconds? Probably less than your GPS receiver's accuracy, unless you're in an aircraft. Most of your "movement" is actually just random noise.
In one of my apps, I save the starting GPS location as a point of reference. On each call to onLocationChanged, I compute the distance between the current location and the point of reference. If the distance is larger than some high-pass value, I add the distance to the running total and set the new location as the point of reference. I use 100 meters as my high-pass value because I've seen a few devices with 100 meter accuracy or worse around tall buildings.
In my app I use the method "DistanceTo()" to get the distance between to places, but don't works correctly!! This is my code for this distance:
double latitudine;
double longitudine;
latitudine = 40.17;
longitudine = 24.9;
Location qui = new Location("Corrente");
qui.setLatitude(latitudine);
qui.setLatitude(longitudine);
double latitudine2;
double longitudine2;
latitudine2 = 40.16;
longitudine2 = 25;
Location due = new Location("Corrente2");
due.setLatitude(latitudine2);
due.setLatitude(longitudine2);
float b = qui.distanceTo(due);
Toast.makeText(getBaseContext(), "DESTINAZIONE KILOMETRI b:" + b, Toast.LENGTH_SHORT).show();
The toast show to me 11077.14 meters, but the distance should be 8.5 km!! as shown in these sites: http://www.movable-type.co.uk/scripts/latlong.html http://www.mapdevelopers.com/distance_from_to.php
In this case the difference is for about 3km, but sometimes the difference is sometimes the difference is even more!! Please help me! Why this code doesn't work correctly? Thanks
you are not calling setLongitude on both Locations, you need:
qui.setLongitude(longitudine);
due.setLongitude(longitudine2);
Might want to take a look at Point2D.Distance instead. It returns
the distance between the two sets of specified coordinates.
This is my first time development in android application. This app will calculate the total distance between two address that input by the user in order to get the total distance. Is it ok to calculate the distance that assume the road is straight? by the way, this application will not show map and does not need GPS! It just needs to use geocoder to get the latitude-longitude.
Here are my questions:
what would the AndroidManifest.xml would look like? do i need to include android:name=".permission.MAPS_RECEIVE".
In the main_activity.java, should i use activity or use fragment activity?
public class MainActivity extends Activity
Thanks for your reply and i would like to appreciate u guys and if possible can you please show the full AndroidManifext.xml code?
Thanks.
http://developer.android.com/reference/android/location/Location.html
Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
or
private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
float pk = (float) (180/3.14169);
float a1 = lat_a / pk;
float a2 = lng_a / pk;
float b1 = lat_b / pk;
float b2 = lng_b / pk;
float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
double tt = Math.acos(t1 + t2 + t3);
return 6366000*tt;
}
You don't need to include the maps permissions.
To get the distance between 2 locations you can use the Geocoder class. This shouldn't need an y location permissions either.
The Geocoder class has a method
getFromLocationName(String locationName, int maxResults)
which returns a list of Addresses for a location input. The addresses have a latitude/longitude.
You need to convert the Address into a Location object which has methods for calculating distance between 2 locations:
distanceTo(Location dest)
Returns the approximate distance in meters between this location and the given location.
As for your second question. You should use Fragments inside of your activity. Have a read through this to understand a bit more about fragments and their advantages.
Use the Geocoder to get the latitude and longitude for place 1 and place 2. Apply those values in the following method.
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results)