package org.r3pek.pharmacies.utils; /* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.util.List; import org.r3pek.pharmacies.R; import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.os.Handler; import android.os.Message; import com.google.android.maps.GeoPoint; /** * Library for some use useful latitude/longitude math */ public class GeoUtils { private static double MILLION = 1000000; /** * Computes the bearing in degrees between two points on Earth. * * @param lat1 Latitude of the first point * @param lon1 Longitude of the first point * @param lat2 Latitude of the second point * @param lon2 Longitude of the second point * @return Bearing between the two points in degrees. A value of 0 means due * north. */ public static float bearing(double lat1, double lon1, double lat2, double lon2) { double lat1Rad = Math.toRadians(lat1); double lat2Rad = Math.toRadians(lat2); double deltaLonRad = Math.toRadians(lon2 - lon1); double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad); double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad); return radToBearing(Math.atan2(y, x)); } /** * Computes the bearing in degrees between two points on Earth. * * @param p1 First point * @param p2 Second point * @return Bearing between the two points in degrees. A value of 0 means due * north. */ public static float bearing(GeoPoint p1, GeoPoint p2) { double lat1 = p1.getLatitudeE6() / MILLION; double lon1 = p1.getLongitudeE6() / MILLION; double lat2 = p2.getLatitudeE6() / MILLION; double lon2 = p2.getLongitudeE6() / MILLION; return bearing(lat1, lon1, lat2, lon2); } /** * Converts an angle in radians to degrees */ public static float radToBearing(double rad) { return (float) ((Math.toDegrees(rad) + 360) % 360); } /** * Returns the current's location address */ public static void getLocationName(final Context context, final Handler h) { Runnable r = new Runnable() { @Override public void run() { Geocoder g = new Geocoder(context); Message m = new Message(); Bundle b = new Bundle(); List
addresses = null; try { addresses = g.getFromLocation(GlobalVars.positioner.getLastLocation().getLatitude(), GlobalVars.positioner.getLastLocation().getLongitude(), 1); } catch (IOException e) { } if (addresses == null || addresses.size() == 0) { b.putString("location", context.getString(R.string.not_available)); m.setData(b); h.dispatchMessage(m); } String result = ""; for (int i = 0; i <= addresses.get(0).getMaxAddressLineIndex(); i++) result += addresses.get(0).getAddressLine(i) + ", "; if (result.equals("")) b.putString("location", context.getString(R.string.not_available)); else b.putString("location", result.substring(0, result.length() - 2)); m.setData(b); h.dispatchMessage(m); } }; Thread t = new Thread(r); t.start(); } }