里面有详细的说明http://www.movable-type.co.uk/scripts/latlong.html
比如计算距离
方法一,
double dist = 0.0; double deltaLat = Math.toRadians(latVal2 - latVal1); double deltaLon = Math.toRadians(lonVal2 - lonVal1); latVal1 = Math.toRadians(latVal1); latVal2 = Math.toRadians(latVal2); lonVal1 = Math.toRadians(lonVal1); lonVal2 = Math.toRadians(lonVal2); double earthRadius = 6371; double a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) + Math.cos(latVal1) * Math.cos(latVal2) * Math.sin(deltaLon/2) * Math.sin (deltaLon/2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); dist = earthRadius * c;
方法二,
public static double calculateHaversineMI(double lat1, double long1, double lat2,double long2) { double dlong = (long2 - long1) * (Math.PI / 180.0f); double dlat = (lat2 - lat1) * (Math.PI / 180.0f); double a = Math.pow(Math.sin(dlat / 2.0), 2) + Math.cos(lat1 * (Math.PI / 180.0f)) * Math.cos(lat2 * (Math.PI / 180.0f)) * Math.pow(Math.sin(dlong / 2.0), 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = 6367* c; return d; }
还有计算方位
double dLong = picLongitude - mLongitude; double y = (Math.sin(dLong) * Math.cos(picLatitude)); double x = (Math.cos(mLatitude) * Math.sin(picLatitude) - Math.sin(mLatitude)*Math.cos(picLatitude)*Math.cos(dLong)); double angleDegreesWrongRange = Math.abs(Math.toDegrees(Math.atan2(y, x))); float angleDegrees = (float) ((angleDegreesWrongRange+360) % 360);
public static String getDirection(float baseAzimuth){ String bearingText = ""; if ( (360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth <= 22.5) ) bearingText = "N"; else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE"; else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E"; else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE"; else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S"; else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW"; else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W"; else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW"; return bearingText; }
都可以在里面找到答案
padding:描述控件里面的内容与控件的关机,内边距;有四个方向属性;paddingleft......
layout_margin:描述控件之间的位置关系,外边距;有四个方向属性;layout_margingleft.....
gravity:设置该控件的对齐方式;
layout_gravity:设置它在父控件中的对齐方式;
尺寸:
FrameLayout(框架布局)
LinearLayout (线性布局)
linearLayout中有一个重要的属性 android:layout_weight="1",这个weight在垂直布局时,代表行距;
水平的时候代表列宽;weight值越大就越大。
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginTop 离某元素上边缘的距离
AbsoluteLayout(绝对布局):
犹如div指定了absolute属性
用X,Y坐标来指定元素的位置
android:layout_x="20px"
android:layout_y="12px"
RelativeLayout(相对布局),
android:layout_above=“@+id/给定的ID” 将该控件的底部至于给定ID的控件之上
TableLayout(表格布局) 。
表格布局类似Html里面的Table。每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素,设定他的对齐方式 android:gravity="" 。
TableLayout置底,TableRow在TableLayout的上面
Button、TextView等控件就在TableRow之上
TableLayout之上也可以单独放控件
android:collapseColumns=0,1:以第0行为序,隐藏指定的列:隐藏第0列,第一列;
android:shrinkColumns:将指定的列设为可收缩的列,该列会收缩至适应屏幕;
android:stretchColumns:将指定的列设为可伸展的列,该列会尽量伸展以填满空间
android:layout_column:控件在TableRow中所处的列
android:layout_span:控件所跨越的列数
这五个布局元素可以相互嵌套应用,做出美观的界面。
Layout用到的一些重要的属性
android:textSize 指定控件当中字体的大小
android:background 指定该控件所使用的背景色,RGB命名法
android:width 指定控件的宽度
android:height 指定控件的高度
android:layout_width 指定Container组件的宽度
android:layout_height 指定Container组件的高度
android:layout_weight View中很重要的属性,按比例划分空间
android:sigleLine 如果设置为真的话,则控件的内容在同一行中进行显示
android:scaleType 是控制图片如何resized/moved来匹对ImageView的siz
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
EditText的android:hint 设置EditText为空时输入框内的提示信息。
android:scaleType: 是控制图片如何resized/moved来匹对ImageView的size。
ImageView.ScaleType / android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
/** * Retrieve accurate location from GPS or network services. * * * Class usage example: * * public void onCreate(Bundle savedInstanceState) { * ... * my_location = new MyLocation(); * my_location.init(main.this, locationResult); * } * * * public LocationResult locationResult = new LocationResult(){ * @Override * public void gotLocation(final Location location){ * // do something * location.getLongitude(); * location.getLatitude(); * } * }; */ public class MyLocations{ /** * If GPS is enabled. * Use minimal connected satellites count. */ private static final int min_gps_sat_count = 5; /** * Iteration step time. */ private static final int iteration_timeout_step = 500; LocationResult locationResult; private Location bestLocation = null; private Handler handler = new Handler(); private LocationManager myLocationManager; public Context context; private boolean gps_enabled = false; private int counts = 0; private int sat_count = 0; private Runnable showTime = new Runnable() { public void run() { boolean stop = false; counts++; System.out.println("counts=" + counts); //if timeout (1 min) exceeded, stop tying if(counts > 120){ stop = true; } //update last best location bestLocation = getLocation(context); //if location is not ready or don`t exists, try again if(bestLocation == null && gps_enabled){ System.out.println("BestLocation not ready, continue to wait"); handler.postDelayed(this, iteration_timeout_step); }else{ //if best location is known, calculate if we need to continue to look for better location //if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec) if(stop == false && !needToStop()){ System.out.println("Connected " + sat_count + " sattelites. continue waiting.."); handler.postDelayed(this, iteration_timeout_step); }else{ System.out.println("#########################################"); System.out.println("BestLocation finded return result to main. sat_count=" + sat_count); System.out.println("#########################################"); // removing all updates and listeners myLocationManager.removeUpdates(gpsLocationListener); myLocationManager.removeUpdates(networkLocationListener); myLocationManager.removeGpsStatusListener(gpsStatusListener); sat_count = 0; // send best location to locationResult locationResult.gotLocation(bestLocation); } } } }; /** * Determine if continue to try to find best location */ private Boolean needToStop(){ if(!gps_enabled){ return true; } else if(counts <= 4){ return false; } if(sat_count < min_gps_sat_count){ //if 20-25 sec and 3 satellites found then stop if(counts >= 40 && sat_count >= 3){ return true; } return false; } // } return true; } /** * Best location abstract result class */ public static abstract class LocationResult{ public abstract void gotLocation(Location location); } /** * Initialize starting values and starting best location listeners * * @param Context ctx * @param LocationResult result */ public void init(Context ctx, LocationResult result){ context = ctx; locationResult = result; myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); bestLocation = null; counts = 0; // turning on location updates myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener); myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener); myLocationManager.addGpsStatusListener(gpsStatusListener); // starting best location finder loop handler.postDelayed(showTime, iteration_timeout_step); } /** * GpsStatus listener. OnChainged counts connected satellites count. */ public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() { public void onGpsStatusChanged(int event) { if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){ try { // Check number of satellites in list to determine fix state GpsStatus status = myLocationManager.getGpsStatus(null); Iterable<GpsSatellite>satellites = status.getSatellites(); sat_count = 0; Iterator<GpsSatellite>satI = satellites.iterator(); while(satI.hasNext()) { GpsSatellite satellite = satI.next(); System.out.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation()); sat_count++; } } catch (Exception e) { e.printStackTrace(); sat_count = min_gps_sat_count + 1; } System.out.println("#### sat_count = " + sat_count); } } }; /** * Gps location listener. */ public final LocationListener gpsLocationListener = new LocationListener(){ public void onLocationChanged(Location location){ } public void onProviderDisabled(String provider){} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider, int status, Bundle extras){} }; /** * Network location listener. */ public final LocationListener networkLocationListener = new LocationListener(){ public void onLocationChanged(Location location){ } public void onProviderDisabled(String provider){} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider, int status, Bundle extras){} }; /** * Returns best location using LocationManager.getBestProvider() * * @param context * @return Location|null */ public static Location getLocation(Context context){ System.out.println("getLocation()"); // fetch last known location and update it try { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); String strLocationProvider = lm.getBestProvider(criteria, true); System.out.println("strLocationProvider=" + strLocationProvider); Location location = lm.getLastKnownLocation(strLocationProvider); if(location != null){ return location; } return null; } catch (Exception e) { e.printStackTrace(); return null; } } }