当前位置: 代码迷 >> Android >> android定位失去城市
  详细解决方案

android定位失去城市

热度:36   发布时间:2016-04-28 01:17:32.0
android定位得到城市

android通过百度地图获取经纬度和具体地址。

官方网站上就有Demo,http://developer.baidu.com/map/index.php?title=androidsdk

可以参考,打开定位的demo,把它的

1.jar包导入,

2.在AndroidManifest中

? ? ? ? ? a.把uses-permission复制进去,

? ? ? ? ? b.申请key~并复制<meta-data

? ? ? ? ? ? ? ? android:name="com.baidu.lbsapi.API_KEY"

? ? ? ? ? ? ? ? android:value="输入你的key" />

? ? ? ? ? c.因为自己定义了类继承Application,所以在Application处加上

? ? ? ? ? ? ? ? ? ? ? android:name="自己定义的类名~"

3.就可以写代码了。

? ? ?MainActivity:

public class MainActivity extends Activity{	private LocationClient mLocationClient;        //显示城市	private TextView LocationResult;        //开始按钮	private Button startLocation;	public void init(){		startLocation = (Button)findViewById(R.id.addfence);		LocationResult = (TextView)findViewById(R.id.textView1);	}	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.location);		init();				mLocationClient = ((LocationApplication)getApplication()).mLocationClient;                  //调用LocationApplication,获得需要的信息		 ((LocationApplication)getApplication()).mLocationResult = LocationResult;				startLocation.setOnClickListener(new OnClickListener() {				@Override			public void onClick(View v) {				// TODO Auto-generated method stub				InitLocation();				mLocationClient.start();		}						}		});	}	@Override	protected void onStop() {		mLocationClient.stop();		super.onStop();	}	private void InitLocation(){		LocationClientOption option = new LocationClientOption();		option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式		//LocationMode.Hight_Accuracy 高精度定位模式下,会同时使用GPS、Wifi和基站定位,返回的是当前条件下精度最好的定位结果		option.setCoorType("gcj02");//返回的定位结果是百度经纬度,默认值gcj02		//可选项:"gcj02"国策局加密经纬度坐标		//"bd09ll"百度加密经纬度坐标		//"bd09"百度加密墨卡托坐标				option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以		mLocationClient.setLocOption(option);	}}

LocationAplication类:

public class LocationApplication extends Application {	public LocationClient mLocationClient;//定位SDK的核心类	public MyLocationListener mMyLocationListener;//定义监听类		public TextView mLocationResult,logMsg;	public static String city=null;	@Override	public void onCreate() {		super.onCreate();		mLocationClient = new LocationClient(this.getApplicationContext());		mMyLocationListener = new MyLocationListener();		mLocationClient.registerLocationListener(mMyLocationListener);	}		/**	 * 实现实位回调监听	 */	public class MyLocationListener implements BDLocationListener {		@Override		public void onReceiveLocation(BDLocation location) {			//Receive Location 						if(location.getCity()!=null){				city=location.getCity();			}						StringBuffer sb = new StringBuffer(256);			if (location.getLocType() == BDLocation.TypeGpsLocation){//定位结果描述:GPS定位结果				sb.append(city);//地理位置			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){//定位结果描述:网络定位结果				sb.append(city);			}			logMsg(sb.toString());		}	}		/**	 * 显示请求字符串	 * @param str	 */	public void logMsg(String str) {		try {			if (mLocationResult != null)				mLocationResult.setText(str);		} catch (Exception e) {			e.printStackTrace();		}	}}

?

  相关解决方案