import java.util.ArrayList;
import java.util.List;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.location.Poi;
import com.bwie.adapter.MyAdapter;
import com.bwie.adapter.MyBaseAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener{
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;
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
private TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
mLocationClient.registerLocationListener( myListener ); //注册监听函数
name = (TextView) findViewById(R.id.name);
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(MainActivity.this,letterToCity,letter));
lv.setOnItemClickListener(this);
lv1=(ListView)findViewById(R.id.listView2);
lv1.setAdapter(new MyBaseAdapter(MainActivity.this,letter));
lv1.setOnItemClickListener(this);
mLocationClient.start();
}
@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){
Toast.makeText(this, letterToCity.get(position), Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "122222222222", Toast.LENGTH_SHORT).show();
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;
}
}
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);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());// 单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");
name.setText(location.getStreet()+"");
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());// 位置语义化信息
List<Poi> list = location.getPoiList();// POI数据
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
}
}
Log.i("BaiduLocationApiDem", sb.toString());
}
}
}
///activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.citytest.MainActivity" >
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="我的位置"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:scrollbars="none"
android:layout_below="@id/name" >
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/name"
android:cacheColorHint="#00000000"
android:divider="@null"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
//city
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="#ffffff"
android:gravity="center_vertical" >
<TextView
android:id="@+id/cityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
//lettter_list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/letterListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#3aa2cf" />
</RelativeLayout>
//letter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="#eeeeee"
android:gravity="center_vertical" >
<TextView
android:id="@+id/letterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
//myadapret
package com.bwie.adapter;
import java.util.List;
import com.example.citytest.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
final static int TYPE_1=1;
final static int TYPE_2=2;
Context context;
List<String> letterToCity;
private String[] letter;
public MyAdapter(Context context, List<String> letterToCity, String[] letter) {
super();
this.context = context;
this.letterToCity = letterToCity;
this.letter=letter;
}
@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) {
ViewHolder1 vh1=null;
ViewHolder2 vh2=null;
int type=getItemViewType(position);
if(convertView==null){
switch (type) {
case TYPE_1:
convertView=LayoutInflater.from(context).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(context).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;
}
class ViewHolder{
TextView tv;
LinearLayout ll;
}
class ViewHolder1{
TextView tv;
}
class ViewHolder2{
TextView tv;
}
}
//mybase
package com.bwie.adapter;
import com.example.citytest.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private String[] letter;
public MyBaseAdapter(Context context, String[] letter) {
super();
this.context = context;
this.letter = letter;
}
@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(context).inflate(R.layout.letter_list, null);
TextView tv=(TextView)view.findViewById(R.id.letterListTextView);
tv.setText(letter[position]);
return view;
}
}