`

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

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

 

分享到:
评论

相关推荐

    Android 简易定位 Location 附

    NULL 博文链接:https://yypdc.iteye.com/blog/2009070

    Android简洁天气项目源码.zip

    15. **通知和定位服务**:天气应用可能利用Android的通知系统向用户推送天气更新,并通过Google Play Services的Fused Location Provider API获取用户位置。 通过分析这个项目源码,开发者可以学习到Android应用...

    android版简易计步器源码

    在Android平台上,开发一...通过学习和实践这个"android版简易计步器源码",开发者不仅能掌握Android传感器的基本用法,还能深入了解如何将硬件数据转化为用户可感知的功能,这对于进一步深入Android开发具有重要意义。

    一个基于Android studio开发的简易天气预报APP源码

    这个开源项目是一个基于Android Studio开发的简易天气预报应用程序。它为用户提供本地城市的实时天气信息,可能包括温度、湿度、风速、空气质量等关键指标。在深入探讨其技术细节之前,让我们先了解一下Android ...

    Android源码——简易音乐播放器源码.zip

    通过分析这个简易音乐播放器的源码,开发者可以学习到Android应用程序的基本架构,以及如何处理多媒体数据,实现音乐播放功能。这对于提升Android开发技能和理解Android系统的工作原理大有裨益。

    基于Android studio的简易天气预报APP源代码,大学生实训项目

    总的来说,这个基于Android Studio的简易天气预报APP项目涵盖了Android应用开发的核心知识,包括UI设计、网络编程、数据解析、本地存储以及测试与发布等多个方面。通过这个实训项目,大学生可以深入理解和掌握...

    带响铃提醒的简易备忘录源码.zip

    这个“带响铃提醒的简易备忘录源码”是一个专为安卓平台设计的应用程序源码,它提供了创建、管理以及设置提醒功能的备忘录工具。作为一个安卓开发者或者学习者,了解并研究这个源码可以帮助你深入理解如何在安卓平台...

    百度地图源码

    【百度地图源码】是一个基于百度地图API开发的简易地图应用源代码,主要适用于Android平台。这个源码项目是用Eclipse开发环境编写的,因此开发者需要熟悉Eclipse IDE来理解和修改代码。通过分析和学习这个源码,我们...

    ibeacon-location.zip

    android源码 BluetoothLe_ibeacon.zip 来源于网络, http://blog.csdn.net/hellogv/article/details/24661777(并且加上自己的一些理解修改而来) APP功能介绍:可以接收到蓝牙基站的RSSI值,并且利用了简单的最小...

    SimpleSpeedometer:适用于Android的车速表

    在这一背景下,我们关注到一款名为"SimpleSpeedometer"的项目,它是一个专为Android设计的简易车速表应用。这款应用以直观、易用为特点,为用户提供实时的行驶速度显示,同时兼容公里/小时(km/h)和英里/小时(mph)两...

Global site tag (gtag.js) - Google Analytics