关于gps Location为空值的问题
tv1=(TextView)findViewById(R.id.textView1); tv2=(TextView)findViewById(R.id.textView2); tv3=(TextView)findViewById(R.id.textView3); locMan = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); getLocationPrivider(); if(mLocation!=null){// gp1=getGeoByLocation(mLocation);// if(listmap.size()!=0){// for(int i=0;i<listmap.size();i++){// String coordinate=map.get("coordinate"); double gp2_lar=170.1; double gp2_long=170.2; double gp1_lar=170.11; double gp1_long=170.2;// Location loc=new Location(null); mLocation.setLatitude(gp2_lar); mLocation.setLongitude(gp2_long); gp2=getGeoByLocation(mLocation); mLocation.setLatitude(gp1_lar); mLocation.setLongitude(gp1_long); gp1=getGeoByLocation(mLocation); double d=getdistance(gp1,gp2); if(d<=100.00){ tv1.setText(gp1_lar+"."+gp1_long); tv2.setText(gp2_lar+"."+gp2_long); tv3.setText("相隔"+d+"米"); }else{ tv1.setText(gp1_lar+"."+gp1_long); tv2.setText(gp2_lar+"."+gp2_long); tv3.setText("相隔"+d+"米"); }// }// } }else{ tv1.setText("mLocation为空"); tv2.setText("mLocation为空"); tv3.setText("mLocation为空"); } /** * 取得GeoPoint的方法 * @param location * @return gp */ private GeoPoint getGeoByLocation(Location location){ GeoPoint gp=null; try{ if(location!=null){ double geoLatitude=location.getLatitude()*1E6; double geoLongitude=location.getLongitude()*1E6; gp=new GeoPoint((int)geoLatitude,(int)geoLongitude); } }catch(Exception e){ e.printStackTrace(); } return gp; } /** * 取得LocationProvider */ public void getLocationPrivider(){ Criteria mc=new Criteria(); mc.setAccuracy(Criteria.ACCURACY_FINE); mc.setAltitudeRequired(false); mc.setBearingRequired(false); mc.setCostAllowed(true); mc.setPowerRequirement(Criteria.POWER_LOW); mLPrivider=locMan.getBestProvider(mc,true); mLocation=locMan.getLastKnownLocation(mLPrivider); }/** * 获取两点之间距离的方法,得出的距离是m */ public double getdistance(GeoPoint gp1,GeoPoint gp2){ double Lat1r=ConvertDegreeToRadians(gp1.getLatitudeE6()/1E6); double Lat2r=ConvertDegreeToRadians(gp2.getLatitudeE6()/1E6); double Long1r=ConvertDegreeToRadians(gp1.getLongitudeE6()/1E6); double Long2r=ConvertDegreeToRadians(gp2.getLongitudeE6()/1E6); //地球半径(km) double R=6371; //计算得出是m double d=Math.acos(Math.sin(Lat1r)*Math.sin(Lat2r)+Math.cos(Lat1r)*Math.cos(Lat2r)*Math.cos(Long2r-Long1r))*R; return d; } /** * ConvertDegreeToRadians */ private double ConvertDegreeToRadians(double degrees){ return (Math.PI/180)*degrees; }