From: Eugene Crosser Date: Thu, 2 Feb 2012 14:02:17 +0000 (+0400) Subject: compute distance (wip) X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=44b9534a0dc3709450e434089b09a6039275d512;p=WhereAmI.git compute distance (wip) --- diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 4463e72..1dc3f55 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -10,4 +10,28 @@ 1200000 3600000 + + дома + на работе + в Москве + в отъезде + + + 55.79214 + 55.65619 + 55.743 + 0 + + + 37.56634 + 37.5261 + 37.614 + 0 + + + 1 + 1 + 25 + 0 + diff --git a/src/org/average/whereami/LastLocation.java b/src/org/average/whereami/LastLocation.java index 230b848..914a615 100644 --- a/src/org/average/whereami/LastLocation.java +++ b/src/org/average/whereami/LastLocation.java @@ -2,7 +2,7 @@ package org.average.whereami; import org.average.whereami.CredentialStore; -//import java.lang.Long; +import java.lang.Math; import com.google.api.client.extensions.android2.AndroidHttp; import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse; @@ -18,6 +18,7 @@ import com.google.api.services.latitude.model.Location; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Resources; import android.util.Log; public final class LastLocation extends Oracle { @@ -26,7 +27,17 @@ public final class LastLocation extends Oracle { private Latitude latitude; - public LastLocation(final CredentialStore store) { + private String[] loc_names; + private String[] loc_lats; + private String[] loc_lons; + private String[] loc_dists; + + public LastLocation(final Resources res, final CredentialStore store) { + loc_names = res.getStringArray(R.array.loc_names); + loc_lats = res.getStringArray(R.array.loc_lats); + loc_lons = res.getStringArray(R.array.loc_lons); + loc_lons = res.getStringArray(R.array.loc_dists); + HttpTransport transport = AndroidHttp.newCompatibleTransport(); JsonFactory jsonFactory = new JacksonFactory(); AccessTokenResponse token = store.read(); @@ -85,6 +96,34 @@ public final class LastLocation extends Oracle { private String locationMessage(Double lat, Double lon, Long tsm) { Log.v(TAG, "entering locationMessage(" + lat + "," + lon + "," + tsm + ")"); - return "Location: " + lat + "N " + lon + "E at " + tsm/1000; + String locmsg = ""; + for (int i = 0; i < loc_names.length; i++) { + Double dist = haversine(lat, lon, + Double.parseDouble(loc_lats[i]), + Double.parseDouble(loc_lons[i])); + Log.v(TAG, "Dist from " + loc_names[i] + + " is " + dist); + //if ((loc_dists[i] == "0") || + // (dist < Double.parseDouble(loc_dists[i]))) { + // locmsg = loc_names[i]; + // break; + //} + } + return locmsg; + } + + private Double toRad(Double deg) { + return deg * 3.14159265 / 180; + } + + private Double haversine(Double lat1, Double lon1, + Double lat2, Double lon2) { + Double dlat = toRad(lat2 - lat1); + Double dlon = toRad(lon2 - lon1); + Double a = Math.sin(dlat / 2) * Math.sin(dlat / 2) + + Math.sin(dlon / 2) * Math.sin(dlon / 2) * + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)); + Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + return 6371 * c; } } diff --git a/src/org/average/whereami/PhoneLog.java b/src/org/average/whereami/PhoneLog.java index 8522e84..dbb7353 100644 --- a/src/org/average/whereami/PhoneLog.java +++ b/src/org/average/whereami/PhoneLog.java @@ -22,6 +22,7 @@ import com.google.api.services.calendar.model.Events; import com.google.common.collect.Lists; import android.content.Context; +import android.content.res.Resources; import android.content.SharedPreferences; import android.util.Log; @@ -31,7 +32,7 @@ public final class PhoneLog extends Oracle { private Calendar calendar; - public PhoneLog(final CredentialStore store) { + public PhoneLog(final Resources res, final CredentialStore store) { HttpTransport transport = AndroidHttp.newCompatibleTransport(); JsonFactory jsonFactory = new JacksonFactory(); AccessTokenResponse token = store.read(); diff --git a/src/org/average/whereami/WhereAmI.java b/src/org/average/whereami/WhereAmI.java index e78cb04..959bd0e 100644 --- a/src/org/average/whereami/WhereAmI.java +++ b/src/org/average/whereami/WhereAmI.java @@ -213,9 +213,9 @@ public class WhereAmI extends Activity tvs = (TextView)findViewById(R.id.timestamp); ut = new UpdateTarget[] { new UpdateTarget((TextView)findViewById(R.id.location), - new PhoneLog(store)), + new PhoneLog(res, store)), new UpdateTarget((TextView)findViewById(R.id.phonecall), - new LastLocation(store)) + new LastLocation(res, store)) }; Log.w(TAG, "created UI, about to start update task"); mHandler.post(updateClock);