i want to create an activity which will restrict the users to access the activity inside some location parameters. if they are outside then they cannot access the activity. The location parameter is big or large area, so access outside even upto 50Km is not an issue. but how to make it restrict inside the particular confinement?
Edit:
i am asking how to define the location restriction?, i am asking if the location is 100N and 100E, so how will i restrict, or can i restrict it using places name?
can anyone explain with some example or a bit of code!?
if you integrated the location api, you can use this to calculate the distance between your particular location and the current location
Location currentLoc;
float radius = 50.0;
float distance = loc.distanceTo(PARTICULAR_LOCATION);
if (distance < radius) {
//start activity
}
here the PARTICULAR_LOCATION is the location of the some area you mentiond
You can calculate the Distance between the Location of the Person and the Location with parameters as
Location personLocation =...; // Person's Location
Location restrictedLocation =...; // Location with constraints
// Calculate distance in meters to person's location from restricted location
float distance = restrictedLocation.distanceTo(personLocation);
float distanceInKms = distance / 1000;
if (distanceInKms > 50) {
// Place code for person outside restricted location's 50 KM radius
} else {
// Place code for person inside restricted location's 50 KM radius
}
In your onLocationChanged you can check if a user is inside a given location or not
#Override
public void onLocationChanged(Location location) {
float[] distance = new float[2];
Location.distanceBetween( location.getLatitude(), location.getLongitude(),
locationtomonitor.latitude, locationtomonitor.longitude, distance);
/// distance in meters
if( distance[0] > 1 ){
Toast.makeText(getBaseContext(), " Outside, Distance in meters: " + locationtomonitor.getRadius(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside, distance in meters : " + distance[0] , Toast.LENGTH_LONG).show();
}
}
if you want to check a specific country or city, you can use IP to location:
just send a simple get Request to http://ip-api.com/json url and you will get a json data containing info about country, city, zip code etc.
Related
i trying to get if location is in my radius.
i.e I have my current location "LatLng" object and i have one more "LatLng" object and i want to check if the two object are in rang of 1km?
How can i implement that?
In Location.distanceBetween() function provide you distance in meters and float value ..
distanceBetween(double startLatitude, double startLongitude, double
endLatitude, double endLongitude, float[] results) Computes the
approximate distance in meters between two locations, and optionally
the initial and final bearings of the shortest path between them.
use this it working i've already checked it .....
float[] dist = new float[1];
Location.distanceBetween(firstLoaction.latitude,firstLoaction.longitude,anotherLocation.latitude,anotherLocation.longitude,dist);
if(dist[0]/1000 > 1){
//here your code or alert box for outside 1Km radius area
}
NOTE:- For getting the location distance always use Location.distanceBetween() which is provide by ANDROID .
double distanceInKiloMeters = (currentLocation.distanceTo(someLocation)) / 1000; // as distance is in meter
if(distanceInKiloMeters <= 1) {
// It is in range of 1 km
}
else {
// not in range of 1 km
}
you can try converting LatLng to Location object for both first and then using distanceTo method to find the distance between those two and check if it is 1km or not
distanceto methode to get distance from locCenter and point and just substitute this distance from the radius if <0 so the point out of range , else the point in border or inside the range.. good luck
I am calculating distance between two location using latitude and longitude... i am getting the distance easily and converting it into miles... but first difference value come in wrong format and other value coming right.
First distance is coming like this = 2.0E-4
While other like this = 48.8881
i have used the following code
Location l1 = new Location("One");
l1.setLatitude(cur_latitude);
l1.setLongitude(cur_longitude);
for (int i = 0; i < lctn.size(); i++) {
Location l2 = new Location("LocationB");
l2.setLatitude(lctn.get(i).getLocation_lat());
l2.setLongitude(lctn.get(i).getLocation_lng());
float distance = l1.distanceTo(l2);
distance =Float.parseFloat(new DecimalFormat("##.####").format( distance / 1000.0f));
Double mile = Double.parseDouble(new DecimalFormat("##.####").format(distance * 0.6214));
Log.e("km_", "" + distance);
Log.e("miles_", "" + mile);
Also please check whether i am using right formula for calculating miles....Thanks in Advance
Distance is in points that why it happening, You need to use accuracy variable and assign some value to this variable,
in my case i use
int accuracy = 20;
Then compare this accuracy with Location accuracy
if(l2.getAccuracy() <= accuracy ) {
Log.e("km_", "" + distance);
Log.e("miles_", "" + mile);
}
I suggest you to use fused location api for accurate distance.
I have used the following code to check GPS coordinates, but problem is that if i am standing at same place the coordinates changes and distance is anywhere between 4 to 20 mts.
I want to change it only when I have moved min 10 mtrs.
locationManager_gps = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager_gps.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
new MyLocationListener_gps());
class MyLocationListener_gps implements LocationListener {
public void onLocationChanged(Location location) {
clat = location.getLatitude();
clon = location.getLongitude();
if (clat != plat || clon != plon) {
float[] results = new float[3];
Location.distanceBetween(plat, plon, clat, clon, results);
if (flag_gps == 0) {
flag_gps = 1;
} else {
GeoPoint geoPoint = new GeoPoint((int) (clat * 1E6),
(int) (clon * 1E6));
mapView.getController().animateTo(geoPoint);
draw = new MyOverLay(geoPoint);
mapView.getOverlays().add(draw);
dist_mtr += results[0];
}
plat = clat;
plon = clon;
}
}
If I use 50 as min distance between updates then it is working fine. I also tried making it 30 but also data was wrong over a period of 4 km while traveling in car.
Please suggest what I should do.
I've seen this too, when taking several location readings and I haven't moved. You can get variable results with different accuracies.
You could try taking, say, 10 location readings and take the one with the best accuracy and disregard the rest. Then when taking the next reading, make sure it is located at a distance which is twice the accuracy of the previous location. For example, if your first location has an accuracy of, say 16 meters, make sure the next reading is at least 32 meters away from the previous location AND has an accuracy better than 32 meters.
You need to use the minTime and minDistance in combination. You pass zero and zero for both, so the min time between updates will be at least zero seconds and the min distance will be zero. So set minTime to a reasonable time and minDistance to 10 for ten meters.
locationManager_gps.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
new MyLocationListener_gps());
There is no magic bullet to solve this... the gps is not 100% accurate and you will always have different readings every time you get a new location update.
You can minimize the issue using a low pass filter for the location values:
clat = plat + (clat-plat) * 0.2; // you should adjust the 0.2 value to the best results
clon = plon + (clon-plon) * 0.2;
....
//your code
....
plat = clat;
plon = clon;
With this the effect of sudden changes to the values will be minimized, which is good for the fake changes in position, but it will also delay the response to the real changes in the positions when the device is moving. You should choose careffuly the value of the constant multiplying (0.2 above) to have the best results.
You can even do better, using a variable instead of a constant, and adjust the value of it based on the accuracy of the loaction update (good accuracy you make the variable close to 1, pour accuracy you make the variable close to 0)
good luck
I am fairly new to Android programming, but I am getting pretty good at it (I think: ))
What I am doing is building a situated stories app. It is an app that places audio files at certain GPS markers and enables the user to listen to them at specific locations.
The next step is moving audio files. What I want to do is set a marker at a specific position in a city. (done). Next I want to check the location of a second marker that moves in a circle around it.
What I have so far is this:
public void checkCircularPosition(){
/*
* could be a solution?
*
radius = 250; //offset in meters
gpsLatCenter = 5.1164; //?how can i make this accurate in meters?
gpsLonCenter = 52.0963; //??how can i make this accurate in meters?
degree = 0; //should be variable over time (full circle in 1Hr, 3600sec --> 360/3600 = 0,1 deg/s)
radian;
radian = (degree/180)*Math.PI;
gpsCircleLat = gpsLatCenter+Math.cos(radian)*radius;
gpsCircleLon = gpsLonCenter-Math.sin(radian)*radius;
*/
}
Now, I have checked this code in adobe flash, which made a movie clip move around in a circle. So I know the calculations are somewhat right. But, I have no way of calculating the latitude and longitude of the resulting coordinates.
EDIT!!
i found the solution with the help posted below. still a lot of work to figure out how to use the results. anyway, i posted the resulting function below.
to make this work, you need _radius wich is 6371 (earth's radius), a bearing, a distance, and a start location.
thanks a lot guys!
public static void destinationPoint(double brng, double dist) {
dist = dist/_radius; // convert dist to angular distance in radians
brng = Math.toRadians(brng); //
double lat1 = Math.toRadians(_lat);
double lon1 = Math.toRadians(_lon);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(brng) );
double lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
lon2 = (lon2+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180ยบ
Log.i(APPTAG, ""+Math.toDegrees(lat2));
Log.i(APPTAG, ""+Math.toDegrees(lon2));
Location movLoc = new Location("");
movLoc.setLatitude(Math.toDegrees(lat2));
movLoc.setLongitude(Math.toDegrees(lon2));
Log.i(APPTAG, ""+movLoc);
}
You should check the section Destination point given distance and bearing from start point at this website: http://www.movable-type.co.uk/scripts/latlong.html
That website has the proper formula for using your start point (gpsLatCenter/gpsLonCenter) and bearing (degree in you code) to compute the final lat/lon (gpsCircleLat/gpsCircleLon).
I am new in android, I want to know how can I calculate the area of a Map in miles on Zoom Level, so that I can call my web service and make a marker according to the distance of my current location.
Your question slightly confusing, area and distance are two different things.
Use the Location.distanceTo function to calculate distance between two points on the map.
OverlayItem item = mOverlays.get(index);
GeoPoint mPoint = item.getPoint(); // get Point of your marker
Location locationMy = m_MyLocationOverlay.getLastFix(); // get current location
if (locationMy != null)
{
Location locationPoint = new Location(locationMy);
locationPoint.setLatitude((double)mPoint.getLatitudeE6() / 1000000.0f);
locationPoint.setLongitude((double)mPoint.getLongitudeE6() / 1000000.0f);
String str = String.format("%.1f miles", locationMy.distanceTo(locationPoint) * 0.000621371192); // meters to miles
}