标签:
百度定位联网权限
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
//使用自己的key值
android:value="hdK3IkF0igS2nQnCIV8MKjRWVnnzyg3A" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
public class MainActivity extends Activity implements OnItemClickListener {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
private TextView tv;
String[] letter = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z" };
String[] cityLetter = { "C", "B", "H", "G", "W", "C", "S", "S", "C", "W",
"H", "Z", "C", "D", "N", "L", "Q", "S", "S", "L", "N", "H", "X",
"H", "S", "Z", "G", "N", "J", "T", "S", "X", "A" };
String[] cityName = { "长沙", "北京", "杭州", "广州", "武汉", "重庆", "上海", "深圳", "长春",
"乌鲁木齐", "哈尔滨", "郑州", "成都", "大连", "南昌", "兰州", "齐齐哈尔", "汕头", "苏州",
"拉萨", "南京", "呼和浩特", "厦门", "合肥", "沈阳", "张家界", "贵州", "宁夏", "济南",
"天津", "石家庄", "西安", "澳门" };
List<String> letterToCity = new ArrayList<String>();
int count;
ListView lv;
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
// LocationClient类是定位SDK的核心类
mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initLocation();
}
});
String str = "";
for (int i = 0; i < letter.length; i++) {
str = letter[i];
boolean isAddLetter = false;
for (int j = 0; j < cityLetter.length; j++) {
if (str.equals(cityLetter[j])) {
if (!isAddLetter) {
letterToCity.add(str);
isAddLetter = true;
}
letterToCity.add(cityName[j]);
}
}
}
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyAdapter());
lv.setOnItemClickListener(this);
lv1 = (ListView) findViewById(R.id.listView2);
lv1.setAdapter(new MyAdapter1());
lv1.setOnItemClickListener(this);
}
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
int span = 1000;
option.setScanSpan(span);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);// 可选,默认false,设置是否使用gps
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
mLocationClient.start();
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
String city = location.getCity();
String street = location.getStreet();
tv.setText(city + "=================" + street);
}
}
class MyAdapter1 extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return letter.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return letter[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.letter_list, null);
TextView tv = (TextView) view.findViewById(R.id.letterListTextView);
tv.setText(letter[position]);
return view;
}
}
class MyAdapter extends BaseAdapter {
final static int TYPE_1 = 1;
final static int TYPE_2 = 2;
@Override
public int getCount() {
// TODO Auto-generated method stub
return letterToCity.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return letterToCity.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
for (int i = 0; i < letter.length; i++) {
if (letterToCity.get(position).equals(letter[i])) {
return TYPE_1;
}
}
return TYPE_2;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* 即使convertView缓存了一些布局,但是在重构时,根本不知道怎么样去让convertView返回你所需要的布局,这时你需
* 要让adapter知道我当前有哪些布局,我重构Item时的布局选取规则,好让convertView能返回你需要的布局。
* 需要重写以下两个函数
*
* @Override public int getItemViewType(int position)
* {}这个函数获取在getView中创建的视图的类型
* @Override public int getViewTypeCount() {}返回在getView中创建视图类型的数量
* 至于这两个方法的详细用处,自己看api即可
*/
ViewHolder1 vh1 = null;
ViewHolder2 vh2 = null;
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_1:
convertView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.letter, null);
vh1 = new ViewHolder1();
vh1.tv = (TextView) convertView
.findViewById(R.id.letterTextView);
convertView.setTag(vh1);
break;
case TYPE_2:
convertView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.city, null);
vh2 = new ViewHolder2();
vh2.tv = (TextView) convertView
.findViewById(R.id.cityTextView);
convertView.setTag(vh2);
break;
default:
break;
}
} else {
switch (type) {
case TYPE_1:
vh1 = (ViewHolder1) convertView.getTag();
break;
case TYPE_2:
vh2 = (ViewHolder2) convertView.getTag();
break;
default:
break;
}
}
switch (type) {
case TYPE_1:
vh1.tv.setText(letterToCity.get(position));
break;
case TYPE_2:
vh2.tv.setText(letterToCity.get(position));
break;
default:
break;
}
return convertView;
// 此方法不可取,刷新时view会刷新
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch (parent.getId()) {
case R.id.listView1:
boolean isLetter = false;
for (int i = 0; i < letter.length; i++) {
if (letter[i].equals(letterToCity.get(position))) {
isLetter = true;
break;
}
}
if (!isLetter) {
tv.setText(letterToCity.get(position));
Toast.makeText(this, letterToCity.get(position),
Toast.LENGTH_SHORT).show();
}
mLocationClient.stop();
break;
case R.id.listView2:
for (int i = 0; i < letterToCity.size(); i++) {
if (letter[position].equals(letterToCity.get(i))) {
lv.setSelection(i);
break;
}
}
break;
default:
break;
}
}
class ViewHolder {
TextView tv;
LinearLayout ll;
}
class ViewHolder1 {
TextView tv;
}
class ViewHolder2 {
TextView tv;
}
}
标签:
原文地址:http://www.cnblogs.com/bokeyuan007/p/5459952.html