`

Android 简易定位 Location 附源码

阅读更多

百度的key实在不明白怎么搞,好恶心。谷歌的解码经纬度地址只能返回到区一级别,剩下都是未知,蛋碎。不知道是我获取方法不对还是地图太老。写着玩玩的,代码有点乱。

package com.location;

import java.io.IOException;
import java.util.List;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	Button showGeroInfoBtn,showGcsInfoBtn,sendSmsBtn;
	TextView showGeroInfoText,showGcsInfoText;
	EditText emcNumEdiTex;
	private double latGps,lonGps,latNet,lonNet;
	Location locationGps,locationNet;
	double geoLat,geoLon;
	String geroAddress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showGeroInfoBtn = (Button) findViewById(R.id.showGeroInfoBtn);
		showGeroInfoText = (TextView) findViewById(R.id.showGeroInfoText);
		showGcsInfoBtn = (Button) findViewById(R.id.showGcsInfoBtn);
		showGcsInfoText = (TextView) findViewById(R.id.showGcsInfoText);
		sendSmsBtn = (Button) findViewById(R.id.sendSmsBtn);
		emcNumEdiTex = (EditText) findViewById(R.id.emcNumEdiTex);
		emcNumEdiTex.setText("18612275213");
		showGcsInfoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				locationGps = locationManager
						.getLastKnownLocation(locationManager.GPS_PROVIDER);
				locationNet = locationManager
						.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
				LocationListener locationListenerGps = new LocationListener() {

					@Override
					public void onStatusChanged(String provider, int status,
							Bundle extras) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onLocationChanged(Location location) {
						// TODO Auto-generated method stub
						locationGps = location;
						latGps = location.getLatitude();
						lonGps = location.getLongitude();

						MainActivity.this.geoLat = latNet;
						MainActivity.this.geoLon = lonNet;

						Log.e("GGGGGGGGG", latGps + "_!!!___!!!_" + lonGps);
						showGcsInfoText.setText("经度:" + latGps + ";" + "纬度:"
								+ lonGps + "Gps");
					}
				};
				LocationListener locationListenerNet = new LocationListener() {

					@Override
					public void onStatusChanged(String provider, int status,
							Bundle extras) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onLocationChanged(Location location) {
						// TODO Auto-generated method stub
						locationNet = location;
						latNet = location.getLatitude();
						lonNet = location.getLongitude();
						MainActivity.this.geoLat = latNet;
						MainActivity.this.geoLon = lonNet;
						Log.e("NNNNNNNNNN", latNet + "_!!!___!!!_" + lonNet);
						showGcsInfoText.setText("经度:" + latNet + ";" + "纬度:"
								+ lonNet + "Net");
					}
				};
				locationManager.requestLocationUpdates(
						locationManager.GPS_PROVIDER, 1000, 0,
						locationListenerGps);
				// locationGps=locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
				locationManager.requestLocationUpdates(
						locationManager.NETWORK_PROVIDER, 1000, 0,
						locationListenerNet);
				// locationNet=locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
				if (locationGps == null && locationNet == null) {
					showGcsInfoText.setText("请确保net或者gps开启!");
				}
			}
		});
		showGeroInfoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				GroAsyncTask groAsyncTask = new GroAsyncTask();
				groAsyncTask.execute();
			}
		});

		sendSmsBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sms = "此为定位求助短信,请即刻报警。我在:"
						+ showGeroInfoText.getText().toString() + "—"
						+ showGcsInfoText.getText().toString();
				SmsManager smsManager = SmsManager.getDefault();
				Log.e("SMSSMSSMS", emcNumEdiTex.getText().toString().trim());
				if (sms.length() > 70) {
	//短信分条,不然超出长度会报错
				List<String> smsList = smsManager.divideMessage(sms);
					for (String smsDivide : smsList) {
						smsManager
								.sendTextMessage(emcNumEdiTex.getText()
										.toString().trim(), null, smsDivide,
										null, null);
					}
				} else {
					smsManager.sendTextMessage(emcNumEdiTex.getText()
							.toString().trim(), null, sms, null, null);
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class GroAsyncTask extends AsyncTask<String, ProgressBar, String> {
//根据经纬度查询地址,返回内容留null会报错,用未知代替
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			Geocoder geocoder = new Geocoder(MainActivity.this);
			Log.e("GEA+GEO", geoLat + "---" + geoLon);
			try {
				List<Address> list = geocoder
						.getFromLocation(geoLat, geoLon, 2);
				for (int i = 0; i < list.size(); i++) {
					Address address = list.get(i);
					if (address.getPremises() != null) {
						if (address.getThoroughfare() != null) {
							if (address.getSubThoroughfare() != null) {
								geroAddress = address.getCountryName()
										+ address.getAdminArea()
										+ address.getLocality()
										+ address.getSubLocality()
										+ address.getPremises()
										+ address.getThoroughfare()
										+ address.getSubThoroughfare();
							} else {
								geroAddress = address.getCountryName()
										+ address.getAdminArea()
										+ address.getLocality()
										+ address.getSubLocality()
										+ address.getPremises()
										+ address.getThoroughfare() + "未知";
							}
						} else {
							geroAddress = address.getCountryName()
									+ address.getAdminArea()
									+ address.getLocality()
									+ address.getSubLocality()
									+ address.getPremises() + "未知" + "未知";
						}
					} else {
						geroAddress = address.getCountryName()
								+ address.getAdminArea()
								+ address.getLocality()
								+ address.getSubLocality() + "未知" + "未知" + "未知";
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return geroAddress;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			showGeroInfoText.setText(result);
		}
	}
}

 地址查询需在已获得经纬度的情况下。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics