不使用地圖jar包擷取當前位置的方法

來源:互聯網
上載者:User

監聽類:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import android.content.Context;import android.location.Criteria;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.util.Log;public class LocationListener implements android.location.LocationListener {private double latitude;private double longitude;private String address;private Context context;private static LocationListener locationListener;public LocationListener(Context context) {this.context = context;}public static LocationListener getInstance(Context context) {if (locationListener == null) {locationListener = new LocationListener(context);}return locationListener;}@Overridepublic void onLocationChanged(Location location) {latitude = location.getLatitude();longitude = location.getLongitude();}@Overridepublic void onProviderDisabled(String provider) {Log.e("open", provider);}@Overridepublic void onProviderEnabled(String provider) {Log.e("close", provider);}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}/** * Set Location Listener *  * @param context */public void setLocationListener() {try {LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);Criteria mCriteria = new Criteria();mCriteria.setAccuracy(Criteria.ACCURACY_FINE);mCriteria.setAltitudeRequired(false);mCriteria.setBearingRequired(false);mCriteria.setPowerRequirement(Criteria.POWER_MEDIUM);if (locationManager.getBestProvider(mCriteria, true) != null) {locationManager.requestLocationUpdates(locationManager.getBestProvider(mCriteria, true), 1000, 0,locationListener);Logging.e("addLocationListener", "start listener location");} else {// Try NETWORK ListenerlocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);Logging.e("addLocationListener failed", "don't get location  please open listener location");}} catch (Exception e) {Logging.e("SetLocationListener", e.getMessage());}}/** * Get Address * @return */public String getAddress() {String str=requestAddress();if(str!=null&&!str.equals("")){address=str;}return address;}/** * Request Address *  * @return */@SuppressWarnings("finally")public String requestAddress() {String address = null;// The KEY can fill in anyfinal String key = "TroubleCh_Android";String url = String.format("http://ditu.google.cn/maps/geo?output=csv&key=%s&q=%s,%s", key, latitude, longitude);URL myURL = null;URLConnection httpsConn = null;try {myURL = new URL(url);} catch (MalformedURLException e) {Log.e("", e.getMessage());} finally {try {httpsConn = 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) {String[] retList = data.split(",");if (retList.length > 2 && ("200".equals(retList[0]))) {address = retList[2];address = address.replace("\"", "");}}insr.close();}} catch (IOException e) {Log.e("", e.getMessage());} finally {return address;}}}}

開啟監聽:

// Location ListenerLocationListener.getInstance(getApplicationContext()).setLocationListener();

非同步擷取:

private class GetMyLocation extends AsyncTask<Void, Void, String> {private ProgressDialog progressDialog;public GetMyLocation() {progressDialog = new ProgressDialog(EmergencyContactDetailActivity.this);progressDialog.show();}@Overrideprotected String doInBackground(Void... params) {String address = null;try {address = LocationListener.getInstance(getApplicationContext()).getAddress();} catch (Exception e) {Logging.e("GetMyLocation Error", e.getMessage());}return address;}@Overrideprotected void onPostExecute(String result) {if (progressDialog != null && progressDialog.isShowing()) {progressDialog.cancel();}textview.setText(result);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.