不使用地图jar包获取当前位置的方法

本文详细介绍了如何在Android应用中实现位置监听,包括设置监听器、获取当前位置坐标,并通过HTTP请求异步获取对应地理位置信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

监听类:

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;
	}

	@Override
	public void onLocationChanged(Location location) {
		latitude = location.getLatitude();
		longitude = location.getLongitude();
	}

	@Override
	public void onProviderDisabled(String provider) {
		Log.e("open", provider);
	}

	@Override
	public void onProviderEnabled(String provider) {
		Log.e("close", provider);
	}

	@Override
	public 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 Listener
				locationManager.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 any
		final 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 Listener
		LocationListener.getInstance(getApplicationContext()).setLocationListener();

异步获取:

private class GetMyLocation extends AsyncTask<Void, Void, String> {

		private ProgressDialog progressDialog;

		public GetMyLocation() {
			progressDialog = new ProgressDialog(EmergencyContactDetailActivity.this);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {
			String address = null;
			try {
				address = LocationListener.getInstance(getApplicationContext()).getAddress();
			} catch (Exception e) {
				Logging.e("GetMyLocation Error", e.getMessage());
			}
			return address;
		}

		@Override
		protected void onPostExecute(String result) {
			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.cancel();
			}
		textview.setText(result);
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值