Compare two differents coordinates in SQL (addition SUM and other) - android

I wan't to compare two coordinates in my database, but i can't use the addition or subtraction. The database i use is SQLite, on Android.
Here is my SQL request :
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m, Prix p
WHERE m.numMag=p.numMag AND p.numProduit=1
AND latitude <= SUM(48.453354+0.01)
AND latitude >=SUM(48.453354-0.01)
AND longitude <= SUM(2.306644+0.06)
AND longitude >= SUM(2.306644-0.06);
Of course this request is completely wrong, but it's my last test. Somebody can help me ?
Other information : SUM(generated Float, -/+number has already been set)

SUM is an aggregation function, so it works when you have a GROUP BY. Just remove it . . .
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m, Prix p
WHERE m.numMag=p.numMag AND p.numProduit=1
AND latitude <= (48.453354+0.01)
AND latitude >=(48.453354-0.01)
AND longitude <= (2.306644+0.06)
AND longitude >= (2.306644-0.06);
You should also use proper join syntax and you can simplify using between:
SELECT m.numMag, nomMag, photoMag, URLMag, latitudeMag,
longitudeMag, numEns, numAdr
FROM Mag m join
Prix p
on m.numMag=p.numMag
WHERE p.numProduit=1 and
latitude between (48.453354-0.01) and (48.453354+0.01) and
longitude between (2.306644-0.06) and (2.306644+0.06)

Related

Matching current Location with Database list of location with in 50 miles

i am trying to work on application , in which i have List of location from database and i want to check Which location from database is near to my current location within few miles like 20 miles and list got refreshed automatically with the refined Location and then send notification to user that you have enter to this location...
I have searched over this and come to know about Geofencing But i did not get it properly.. so please suggest me how should i start working on it ... Any sample , code or link will be helpful
thanks in advance
I have used one function distanceTo to calculate distance between to Locations.
I have coded it for only 100 mtr, you need to change value of distance you want to compare with.
private boolean isMarkerIn100(ArrayList<Model> arrayList) {
Location locationA = new Location("LocationA");
locationA.setLatitude(latitude);
locationA.setLongitude(longitude);
for (int i = 0; i < arrayList.size(); i++) {
Location locationB = new Location("LocationB");
locationB.setLatitude(arrayList.get(i).getLat());
locationB.setLongitude(arrayList.get(i).getLng());
if (locationA.distanceTo(locationB) < 100 && arrayList.get(i).getHit() > 4) {
return false;
}
}
return true;
}
In above code, latitude and longitude are for my (User's) location (Say locationA) and ArrayList is data from database with lat and lng.

Android location change Latitude and Longitude from DMS format to double

I have this format for a location (24°13'30.92"N,55°45'51.94"E) and I need to convert it through android to a double value like (24.225275, 55.764417).
I tried something like this and it did not work
String lonStr="24°13'30.92"N";
Double lon=Location.convert(lonStr);
Use substring and indexOf
extract minutes and second divide by 60 and 3600 respectively and
finally add degree part.
String str="24°13'30.92\"N,55°45'51.94\"E";
String s[]=str.split(",");
double lat=Integer.parseInt(s[0].substring(0,s[0].indexOf("°")))+(double)Integer.parseInt(s[0].substring(s[0].indexOf("°")+1, s[0].indexOf("'")))/60+ Double.parseDouble(s[0].substring(s[0].indexOf("'")+1, s[0].indexOf("\"")))/3600;
// same for longitude
System.out.println(String.format("%.6f", lat));
output:
24.225256

How to Compaire Geolocations In Android?

How to Compare saved (in database ) Geo Location(Longitude and Latitude) with currently getting one using GPS.i tried following link but it doesn't work for me ,getting wrong output.Please help me i am new in Android.
How to use coarse location to compare with saved location in database
my requirement is exact as explained in ANS.
i did
float radius = 150; // distance in meter
double latitude_new= gps.getLatitude();
double longitude_new = gps.getLongitude();
Location location1= new Location("gpslocation");
location1.setLatitude(21.xxxxxxx);
location1.setLongitude(78.xxxxxxx);
Location location2= new Location("gpslocation");
location2.setLatitude(latitude_new);
location2.setLongitude(latitude_new);
float distance = location2.distanceTo(location1);
distance=Math.abs(distance);
//comparing two distance and radius
if (distance <= radius){
Toast.makeText(MyActivity.this, "At home", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MyActivity.this,"Not in Home",Toast.LENGTH_SHORT).show();
}
problem is i m getting only "Not in Home" at SAME location which i have hard-coded and even on other locations(other than hard-coded).
thanks
You have provide latitude for the longitude also , edit your code as follows and try ,
location2.setLongitude(longitude_new);

android: create function for sqlite

i need to create a function for sqlite, because the sin and cos mysql-function doesn't exist, and i need to make a query like:
String q = "SELECT * FROM shops WHERE _id > 0 AND distance(lat, lng, latitudine, longitudine) < 20 AND cancellato=0 ORDER BY _id";
Cursor c = myDataBase.rawQuery(q, null);
return c;
so, i need a function for distance.
and i can't do it via code because it's a list of +8000 shops!
how can i? i didn't found anything on the web.
thanks!
distance(lat, lng, latitudine, longitudine)
I think you have to calculate distance pragmatically from your javacode then you can compare
http://www.sqlite.org/lang_aggfunc.html
http://oreilly.com/catalog/sqlnut/chapter/ch04.html

Geo Point vs Location

This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}

Categories

Resources