你的位置:首页 > 操作系统

[操作系统]通过百度地图API定位


1.把百度地图定位API(下载地址:http://lbsyun.baidu.com/sdk/download?selected=location)里面的libs复制到自己的项目libs里面

2.进行相关的配置,具体见官方文档:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0

3.代码:

 1 package com.zzw.baidumap; 2  3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7  8 import android.app.Activity; 9 import android.os.Bundle;10 import android.util.Log;11 12 public class MainActivity extends Activity {13   LocationClient mLocationClient;14 15   @Override16   protected void onCreate(Bundle savedInstanceState) {17     super.onCreate(savedInstanceState);18 //    setContentView(R.layout.activity_main);19 20     // 得到LocationClient21     mLocationClient = new LocationClient(this);22 23     // 得到LocationListenner24     MyLocationListenner mListenner = new MyLocationListenner();25 26     // 设置LocationClient选项27     LocationClientOption option = new LocationClientOption();28     option.setIsNeedAddress(true);29     option.setAddrType("all");30     option.setCoorType("bd09ll");// 设置为百度的定位类型31 32     mLocationClient.setLocOption(option);33 34     // 注册LocationListener35     mLocationClient.registerLocationListener(mListenner);36 37     // 开始启动38     mLocationClient.start();39   }40 41   @Override42   protected void onDestroy() {43     // 结束启动44     mLocationClient.stop();45     super.onDestroy();46   }47 48   // 具体操作49   private class MyLocationListenner implements BDLocationListener {50 51     @Override52     public void onReceiveLocation(BDLocation location) {53       try {54         // 当前设备位置所在的省55         String province = location.getProvince();56         Log.d("province", province);57 58         // 当前设备位置所在的市:比如:成都市59         String city = location.getCity();60         Log.d("city", city);61 62         // 具体地址:从国家到街号-->中国xx省xx市xx区xx路xx号-附x63         String addeStr = location.getAddrStr();64         Log.d("addeStr", addeStr);65 66         // 国家67         String country = location.getCountry();68         Log.d("country", country);69 70         // 区,比如:金牛区71         String district = location.getDistrict();72         Log.d("district", district);73 74         // 街道 比如:西安南路75         String street = location.getStreet();76         Log.d("street", street);77 78         // 具体的街道号数 比如:60号-附279         String streetNumber = location.getStreetNumber();80         Log.d("streetNumber", streetNumber);81 82         // 当前时间83         String time = location.getTime();84         Log.d("time", time);85 86         Log.d("当前位置", addeStr);87       } catch (Exception e) {88         e.printStackTrace();89       }90 91     }92 93   }94 }