android method (distance between())
enter code here
package tryanabtry.opa;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class tryanabtry extends MapActivity{
private MapView mapView;
private MapController mc;
GeoPoint p, p2, p3, p4;
List<Overlay> mapOverlays;
Drawable drawable, drawable2 , drawable3, drawable4;
HelloItemizedOverlay itemizedOverlay, itemizedOverlay2 , itemizedOverlay3, itemizedOverlay4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(12);
addOverLays();
}
catch(Exception e){
Log.d("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",e.getMessage());
}
}
public void addOverLays(){
String [] coordinates = {"31.216487288475037","29.932637214660645" ,"30.084123015403748", "51.5002" , "-0.1262","31.337149143218994"};
double lat = 31.216487288475037;//lat2 = 51.5002,lat3=29.987091422080994;
double log = 29.932637214660645;// log2 = -0.1262,log3=31.43909454345703;
p = new GeoPoint((int) (lat * 1E6), (int) (log * 1E6));
// p2 = new GeoPoint( (int) (lat2 * 1e6), (int) (log2 * 1e6));
// p3=new GeoPoint( (int) (lat3 * 1e6), (int) (log3 * 1e6));
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.ballon);
//drawable2 = this.getResources().getDrawable(R.drawable.ballon);
// drawable3 = this.getResources().getDrawable(R.drawable.ballon);
itemizedOverlay = new HelloItemizedOverlay(drawable,this);
// itemizedOverlay2 = new HelloI开发者_如何学PythontemizedOverlay(drawable2,this);
// itemizedOverlay3 = new HelloItemizedOverlay(drawable3,this);
OverlayItem overlayitem = new OverlayItem(p, "Cairo", " over1");
// OverlayItem over2 = new OverlayItem(p2, "ulm", "over2");
//OverlayItem over3 = new OverlayItem(p3, "offff", "over3");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
// itemizedOverlay2.addOverlay(over2);
// mapOverlays.add(itemizedOverlay2);
// itemizedOverlay3.addOverlay(over3);
// mapOverlays.add(itemizedOverlay3);
mc.setZoom(17);
// mc.animateTo(p);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
GeoPoint point = new GeoPoint( (int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
//DoubletoString(loc.getLatitude());
//DoubletoString(loc.getLongitude());
///////////////////////////////////////////////////
loc.getAccuracy();
////////////////////////////////////////////////////////////
String Text = "My current location is: " +
"Latitud ="+ loc.getLatitude() +
"Longitud =" + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
mc.animateTo(point);
///////////////////////////////////////7ewar el remove
///////////////if (point ==p){
///////////////mapView.getOverlays().remove(p);
////////////////// }
}
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
Location loc = null;
GeoPoint point = new GeoPoint( (int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
lat1=loc.getLatitude();
lon1=loc.getLongitude();
lat2= 31.216487288475037;
lon2=29.932637214660645;
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts decimal degrees to radians :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:: This function converts radians to decimal degrees :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double rad2deg(double rad) {
return (rad / Math.PI * 180.0);
}
private void DoubletoString(double latitude) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
protected boolean isRouteDisplayed() {
return false;
}
}/* End of Class MyLocationListener */
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
/* End of UseGps Activity*/
so here the method distance must get the distance between my current location and other location i enter ?!! is it work or there is something i must add to this method
精彩评论