public class MainActivity extends Activity {
private final String TAG = "BX";
private LocationManager locationManager; private int latitude = 0;// 纬度 private int longitude = 0;// 经度 private LocationListener locationListener; // 表示是否GPS定位成功 private Boolean isGPSSuccessful;
private Button btn;
private String city;
private Handler handler = new Handler() {
@Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); if (msg.what == 0) { city = (String) msg.obj; } } };
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化GPS监听 initLocationListener(); // 初始化locationManager initLocationManager(); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(l); }
private void initLocationManager() { locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); // 设置方向的精确 可选参数有ACCURACY_LOW,低ACCURACY_HIGH 高NO_REQUIREMENT.没有要求 criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置是否需要获取海拔数据 criteria.setAltitudeRequired(false); // 设置是否需要获得方向信息 criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功效 // String provider = locationManager.getBestProvider(criteria, true); String provider = LocationManager.NETWORK_PROVIDER; Log.e(TAG, "priovider=" + provider); Location location = locationManager.getLastKnownLocation(provider); Log.e(TAG, location + ""); // 设置监听器,自动更新的最小时间为间隔1秒,最小位移变化超过5米 locationManager.requestLocationUpdates(provider, 1000, 5, locationListener);
updateNewLocation(location); }
private void initLocationListener() { locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateNewLocation(location); }
public void onProviderDisabled(String provider) { updateNewLocation(null); }
public void onProviderEnabled(String provider) { updateNewLocation(null);
}
public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { // GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i(TAG, "当前GPS状态为可见状态"); break; // GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i(TAG, "当前GPS状态为服务区外状态"); break; // GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i(TAG, "当前GPS状态为暂停服务状态"); break; } } }; }
private void updateNewLocation(Location location) { if (location != null) { latitude = (int) location.getLatitude(); longitude = (int) location.getLongitude(); } else { Log.e(TAG, "定位不成功"); } }
private View.OnClickListener l = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); } };
protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.e(TAG, latitude + ""); getCityName(); // if (resultCode == RESULT_OK) { if (requestCode == 0) { File file = new File("sdcard/PhoneHelper/Picture/"); file.mkdirs();// 创建文件夹 String fileName = "/mnt/sdcard/PhoneHelper/Picture/" + city + ".jpg"; Log.e("BX", "" + fileName); Bundle bundle = data.getExtras(); Bitmap bmp = (Bitmap) bundle.get("data"); try { FileOutputStream out = new FileOutputStream(fileName); bmp.compress(CompressFormat.JPEG, 100, out); Log.e("BX", "保存");
} catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
// } };
public String geocodeAddr(String latitude, String longitude) { String addr = "";
// 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址 // 密钥可以随便写一个key=abc // output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析 String url = String.format( "http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s", latitude, longitude); URL myURL = null; URLConnection httpsConn = null; try { myURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); return null; } try { httpsConn = (URLConnection) myURL.openConnection(); if (httpsConn != null) { InputStreamReader insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); BufferedReader br = new BufferedReader(insr); String data = null; if ((data = br.readLine()) != null) { System.out.println(data); String[] retList = data.split(","); if (retList.length > 2 && ("200".equals(retList[0]))) { addr = retList[2]; addr = addr.replace("\"", ""); } else { addr = ""; } } insr.close(); } } catch (IOException e) { e.printStackTrace(); return null; } return addr; }
private void getCityName() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Message msg = new Message(); msg.what = 0; msg.obj = geocodeAddr(latitude + "", longitude + ""); handler.sendMessage(msg); } }).start(); }
@Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 1000, 5, locationListener); }
@Override protected void onPause() { super.onPause(); locationManager.removeUpdates(locationListener); }
}