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" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<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>
*******************************************************************************************************************
MainActivity.class代码
package com.example.citytest;
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.Poi;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
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;
boolean isFirstLoc = true; // 是否首次定位
LocationClient mLocClient;
public MyLocationListener myListener = new MyLocationListener();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
private static final int accuracyCircleFillColor = 0xAAFFFF88;
private static final int accuracyCircleStrokeColor = 0xAA00FF00;
MapView mMapView;
BaiduMap mBaiduMap;
private TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.name);
mMapView = (MapView)findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mMapView.setVisibility(View.GONE);
mCurrentMode = LocationMode.NORMAL;
mLocClient = new LocationClient(getApplicationContext());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option);
mLocClient.start();
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);
}
@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();
}
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;
}
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
isFirstLoc = false;
location.getCity();
location.getAddress();
location.getAddrStr();
location.getStreet();
location.getCountry();
// Log.i("TAG",location.getCity()+"..............."
// +location.getAddress()+"..............."+location.getAddrStr()+"..............."+
// location.getStreet()+"..............."+location.getCountry());
name.setText(location.getAddrStr());
// name.setText(location.getStreet());
Log.i("aaa", location.getAddrStr());
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(18.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
}
**************************************************************************************************************
MyAdapter适配器
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;
}
}
************************************************************************************
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>
*****************************************************************************************************
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>
*********************************************************************************************************************
MyBaseAdapter适配器
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;
}
}
*********************************************************************************************
letter_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:text="123"
android:textColor="#3aa2cf"
android:textSize="18sp" />
</RelativeLayout>
****************************************
自己定义一个Application
package com.example.citytest;
import android.app.Application;
import com.baidu.mapapi.SDKInitializer;
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(getApplicationContext());
}
}
***************************************************
导入相应的包和jar文件
记得自己去申请key值
需要配置的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.citytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<!-- 这个权限用于进行网络定位-->
<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>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.example.citytest.DemoApplication">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="5TYO3Ar10XRM8cqFNCNz04aQDUScCMyK" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
</application>
</manifest>
记得里面的服务要配置要不不能进行定位