关于LBS、、

来源:互联网 发布:boker plus淘宝 编辑:程序博客网 时间:2024/05/21 05:37
import android.content.Context;
import android.location.*;

/**
 * 定位处理工具类
 */
public class LocationUtil {

/**
* 是否支持GPS,如果未打开则打开GPS
* @param context 上下文对象
* @return true代表支持,false代表未打开,则打开GPS设置界面
*/
public static boolean isSupport(Context context){
LocationManager locationManager ;
// 获取 LocationManager 服务
  locationManager = (LocationManager) context
  .getSystemService(Context. LOCATION_SERVICE );  
boolean b = locationManager .isProviderEnabled(android.location.LocationManager. GPS_PROVIDER )
  || locationManager .isProviderEnabled(android.location.LocationManager. NETWORK_PROVIDER );
return b;
}

/**
  * 获得经度和纬度
  * @param context 上下文对象
  * @return 经纬度数组,下标0是经度,下标1是纬度
  */
  public static String[] getLongitudeAndLatitude(Context context){
  LocationManager locationManager ;
  String provider ;
  Location location ;
  String[] lbsStr = null;
  // 获取 LocationManager 服务
  locationManager = (LocationManager) context
  .getSystemService(Context. LOCATION_SERVICE );  
  // 构建位置查询条件
  Criteria criteria = new Criteria();
  // 查询精度:高
  criteria.setAccuracy(Criteria. ACCURACY_FINE );
  // 是否查询海拨:否
  criteria.setAltitudeRequired( false );
  // 是否查询方位角 : 否
  criteria.setBearingRequired( false );
  // 是否允许付费:是
  criteria.setCostAllowed( true );
  // 电量要求:低
  criteria.setPowerRequirement(Criteria. POWER_LOW );
  // 返回最合适的符合条件的 provider ,第 2 个参数为 true 说明 , 如果只有一个 provider 是有效的 , 则返回当前 provider
  provider = locationManager .getBestProvider(criteria, true );  
   
  // 获取位置
  location = locationManager .getLastKnownLocation(provider);
  if(location != null){
double lng = location.getLongitude();
double lat = location.getLatitude();
 
lbsStr = new String[2];
lbsStr[0] = "" + lng;
lbsStr[1] = "" + lat;
  }
  return lbsStr;
  }

}
原创粉丝点击