android获取坐标并输出完整地址

室内获取GPS信号可能有点问题,出去走两步试试
同时记得在添加系统权限,来支持对LBS硬件的访问
  
Java代码:
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
< /uses-permission>



package com.location;

import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LocationFinder extends Activity {
	/** Called when the activity is first created. */
	static String address = "还没找到地址";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// double latitude=0,longitude=0;

		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				1000, 0, locationListener);

		Location location = locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		while(location==null){
			locationManager.requestLocationUpdates("gps", 60000, 1, locationListener);

			
		}
		
		
		double latitude = location.getLatitude(); // 经度
		double longitude = location.getLongitude(); // 纬度

		StringBuilder sb = new StringBuilder();
		sb.append("经度:").append(String.valueOf(latitude)).append("\n")
				.append("纬度:").append(String.valueOf(longitude)).append("\n");
		// 处理地理编码
		Geocoder gc = new Geocoder(this, Locale.getDefault());
		try {
			List<Address> add = gc.getFromLocation(latitude, longitude, 1);
			StringBuilder bb = new StringBuilder();
			if (add.size() > 0) {
				Address ad = add.get(0);
				bb.append(ad.getAddressLine(0)).append("\n");
				bb.append(ad.getAddressLine(1)).append("\n");
				bb.append(ad.getAddressLine(2)).append("\n");
				sb.append(bb);
			}
		} catch (Exception e) {
		}
		address = sb.toString();
		TextView text = (TextView) findViewById(R.id.tv);
		text.setText(address);
	}

	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) { // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
			// log it when the location changes
			if (location != null) {
				Log.i("SuperMap",
						"Location changed : Lat: " + location.getLatitude()
								+ " Lng: " + location.getLongitude());
			}
		}

		public void onProviderDisabled(String provider) {
			// Provider被disable时触发此函数,比如GPS被关闭
		}

		public void onProviderEnabled(String provider) {
			// Provider被enable时触发此函数,比如GPS被打开
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
		}
	};
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值