-3

I have two strings (latitude and longitude) stored within my database. I only want an activity to start once the device my app is installed on reaches a certain location.

I thought about writing an if-statement (see code below) in order to solve this however it is not working.

Is there another way of doing this?

Could anyone provide me with suggestions?

tvLatitude.setText(String.valueOf(gps.getLatitude()));
tvLongitude.setText(String.valueOf(gps.getLongitude()));
String longi = tvLongitude.getText().toString();
String lati =  tvLatitude.getText().toString();
String WorkLocationlongi = "REQUIRED LONGITUDE";
String WorkLocationlati = "REQUIRED LATITUDE";
mRootRef.child("Longitude").setValue(longi);
mRootRef.child("Latitude").setValue(lati);

if (longi.equals(WorkLocationlati)&& lati.equals(WorkLocationlongi)){
    String dtbClockon = tvClock.getText().toString();
    mRootRef.child("Dave_Clock_on_date").setValue(formattedDateOnly);
    mRootRef .child("Dave_Clock_on").setValue(dtbClockon);
    startActivity(new Intent(getBaseContext(), MainActivity.class));
} 
else {
    Toast.makeText(context,"Not in work area please enter the yard and try again",Toast.LENGTH_SHORT).show();
}

Thanks.

Prad
  • 515
  • 7
  • 15
Mark Harrop
  • 192
  • 1
  • 14
  • 1
    What's gone wrong? Does it compile? Does the if ever eval to true, under what condition is it supposed to eval to true? etc... – George Oct 27 '16 at 10:25
  • 2
    a) Did you confuse long for lat : "if (**longi**.equals(WorkLocation**lati**)&& lati.equals(WorkLocationlongi))" ? b) In that case, long/lat has to be **exactly** the same. You should add some "buffer". The more precise the mobile's GPS is, the harder it will be to hit the exact same position. – Fildor Oct 27 '16 at 10:25
  • 1
    What "wouldn't work"? What is `longi` and `lati` supposed to contain? – QBrute Oct 27 '16 at 10:25
  • sorry will edit to add more details but the new activity is supposed to start but it dosent it just keeps toasting the message not in work area – Mark Harrop Oct 27 '16 at 10:28
  • 1
    Why would you convert latitude/longitude to strings before comparing though? – 1615903 Oct 27 '16 at 10:30
  • because its displayed as text on my login screen and i just grabbed that text. – Mark Harrop Oct 27 '16 at 10:34
  • 1
    If I might, I'd like to suggest using additional or completely other means to determine "user is at workplace" ... 1. GPS fixes may not be available inside of buildings. (Think "basement" or thick walls) 2. For different company perimeter layouts you may have to span up a fence that is not just a point + distance (=round perimeter) - imagine drawing a polygon on the computer. So you actually have to check for your fix to be inside an area spun up by various locations ... 3. Other means could be much much simpler: e.g. Login only possible through company's (local) network? – Fildor Oct 27 '16 at 10:43

3 Answers3

1

I think there is a confusion with the variables being used, based on that assumption, I suggest changing

if (longi.equals(WorkLocationlati)&& lati.equals(WorkLocationlongi))

to

if (longi.equals(WorkLocationlongi)&& lati.equals(WorkLocationlati))
almost a beginner
  • 1,622
  • 2
  • 20
  • 41
  • 3
    Hitting the exact location will also be pretty hard. I guess OP will have to span a little area around that spot that will be considered "in the work area". – Fildor Oct 27 '16 at 10:30
  • then removing a couple of the decimal places worked for me.. i didnt want massive amounts of wokr just something that check they are in the area. thanks for the help @almost – Mark Harrop Oct 27 '16 at 11:01
1

I think the problem is that gps is not accurate. so you will get different value each time even if you are at the same place

you could take the first few decimals of longi, lati, WorkLocationlati and WorkLocationlongi.

NOTE: (more decimals more precise location)

Shaheed Alhelal
  • 186
  • 1
  • 6
  • Well he could as well just compute the distance to the target and consider a certain distance as acceptable (=error tolerance). Using strings makes that harder, though. – Fildor Oct 27 '16 at 10:32
  • i only wanted any easy system lol.. just something that check they are in work before logging on – Mark Harrop Oct 27 '16 at 10:33
  • 1
    @MarkHarrop So what you want is a "GPS fence". You cannot do that with one single specific GPS location. That would be like a Point. You could use a Point and a certain distance to it to cover a round perimeter. However that might not be enough for certain Buildings. You also have to keep in mind that you might not have GPS fixes inside of buildings available. – Fildor Oct 27 '16 at 10:35
  • so can i search gps fence? didnt know what to search for thanks – Mark Harrop Oct 27 '16 at 10:36
  • @MarkHarrop you should calculate the difference between gps location and work location (i.e. if the defiance is less than 10 let the employee sign in). this is the fence they taking about – Shaheed Alhelal Oct 27 '16 at 12:02
1

I believe that you are comparing the wrong values, in the if you are comparing longi with WorkLocationlati. Also I would recommend you to create some range for that, because in the GPS to be the exactly same location will be a little bit complicate. Maybe you should look at this anwers here : Algorithm to find all Latitude Longitude locations within a certain distance from a given Lat Lng location

Community
  • 1
  • 1
Galeixo
  • 102
  • 9