地图 LBS

package com.example.lbs;

import java.util.List;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	TextView txtposition;
	LocationManager locationManager;
	String provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtposition = (TextView) findViewById(R.id.txt);
		// 获取到了 LocationManager 的实例
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// 獲取所有可用的位置提供器
		List<String> providerList = locationManager.getProviders(true);
		if (providerList.contains(LocationManager.GPS_PROVIDER)) {
			provider = LocationManager.GPS_PROVIDER;
		} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
			provider = LocationManager.NETWORK_PROVIDER;
		} else {
			Toast.makeText(this, "No location provider to use",
					Toast.LENGTH_SHORT).show();
			return;
		}
		// 获取到记录 当前位置信息的 Location 对象了
		Location location = locationManager.getLastKnownLocation(provider);
		if (location != null) {
			// 显示当前设备的位置信息
			showLocation(location);
		}
		locationManager.requestLocationUpdates(provider, 5000, 1,
				locationListener);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (locationManager != null) {
			// 关闭程序时将监听器移除
			locationManager.removeUpdates(locationListener);
		}
	}

	LocationListener locationListener = new LocationListener() {

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub

		}

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

		}

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

		}

		@Override
		public void onLocationChanged(Location arg0) {
			// TODO Auto-generated method stub
			// 更新當前設備的位置信息
			showLocation(arg0);
		}
	};

	private void showLocation(Location location) {
		// TODO Auto-generated method stub
		String currentPosition = "latitude is" + location.getLatitude() + "\n"
				+ "longitude is" + location.getLongitude();
		txtposition.setText(currentPosition);
	}

}
</pre><pre name="code" class="html">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



反向地理编码,看得懂的位置信息

Geocoding API 中规定了很多接口,其中反向地理编码的接口如下:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
我 们 来 仔 细 看 下 这 个 接 口 的 定 义 , 其 中
http://maps.googleapis.com/maps/api/geocode/是固定的,表示接口的连接地址。
json 表 示 希 望 服 务 器 能 够 返 回 JSON 格 式 的 数 据 , 这 里 也 可 以 指 定 成 xml。latlng=40.714224,-73.96145表示传递给服务器去解码的经纬值是北纬40.714224度,
西经 73.96145 度。 sensor=true_or_false 表示这条请求是否来自于某个设备的位置传感器,通常指定成 false 即可。
如 果 发 送http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.96145& sensor=false 这样一条请求给服务器,我们将会得到一段非常长的 JSON 格式的数据,其中会包括如下部分内容:
"formatted_address" : "277 Bedford Avenue, 布鲁克林纽约州 11211美国"
从这段内容中我们就可以看出北纬 40.714224 度,西经 73.96145 度对应的地理位置是在哪里了。如果你想查看服务器返回的完整数据,在浏览器中访问上面的网址即可

	private void showLocation(final Location location) {
		// TODO Auto-generated method stub
//		String currentPosition = "latitude is" + location.getLatitude() + "\n"
//				+ "longitude is" + location.getLongitude();
//		txtposition.setText(currentPosition);
		new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					// 组装反向地理编码的接口地址
					StringBuilder url=new StringBuilder();
					url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
					url.append(location.getLatitude()).append(",");
					url.append(location.getLongitude());
					url.append("&sensor=false");
					HttpClient httpClient=new DefaultHttpClient();
					HttpGet http=new HttpGet(url.toString());
					// 在请求消息头中指定语言,保证服务器会返回中文数据
					http.addHeader("Accept-Language","zh-CN");
					HttpResponse httpResponse=httpClient.execute(http);
					if (httpResponse.getStatusLine().getStatusCode()==200) {
						HttpEntity entity=httpResponse.getEntity();
						String response=EntityUtils.toString(entity,"utf-8");
						JSONObject jsonObject=new JSONObject(response);
						
						// 获取results节点下的位置信息
						JSONArray result=jsonObject.getJSONArray("result");
                         if (result.length()>0) {
							JSONObject sub=result.getJSONObject(0);
							
							// 取出格式化后的位置信息
							String address=sub.getString("formatteed_address");
							Message message=new Message();
							message.what=SHOW_LOCATION;
							message.obj=address;
							handler.sendMessage(message);
						}						
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			
			}
		}.start();
	}

	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_LOCATION:
				String currenposition=(String)msg.obj;
				txtposition.setText(currenposition);
				break;

			default:
				break;
			}
		};
	};
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值