码迷,mamicode.com
首页 > 移动开发 > 详细

android使用自定控件实现城市列表展示并且实现当前城市定位

时间:2016-05-06 15:58:16      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

自定义控件,点击控件,展示城市列表,使用百度地图定位当前城市,并且展示当前城市
代码类
package com.example.test0504;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.example.adapter.MyAdapter;
import com.example.test0504.MainActivity.MyLocationListenner;


/**
 * 
 * 定义城市页面
 * @author 浅议爱
 *
 */
public class CityActivity extends Activity implements
    MySideBar.OnTouchingLetterChangedListener {

    //定义控件
    MapView mMapView = null;      
    // 定位相关
    LocationClient mLocClient;
    //配置定位图层显示方式
    private LocationMode mCurrentMode;
    BitmapDescriptor mCurrentMarker;
    //定义 BaiduMap 地图对象的操作方法与接口
    BaiduMap mBaiduMap;
    boolean isFirstLoc = true; // 是否首次定位
    public MyLocationListenner myListener = new MyLocationListenner();

    private String city;



    private ListView mainList;
    /** 数据源 */
    private List<String> data;
    /** 字母位置 */
    private List<Integer> letterPositionList;
    /** 字母Char */
    private List<Integer> letterCharList;
    private MySideBar myView;
    private TextView tv01;
    private String[] title = {"GPS定位城市", "热门城市", "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" };
    private int lastFirstVisibleItem;


  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.city_activity);

    //或得上一个页面传递过来的城市名称(这里指的是当前定位的城市名)
//  Intent it=getIntent();
//  final String tv=(String) it.getSerializableExtra("city");


     mMapView=(MapView) findViewById(R.id.bmapView);
        mLocClient = new LocationClient(this);
     mLocClient.registerLocationListener(myListener);
     mBaiduMap=mMapView.getMap();
     // 开启定位图层
     mBaiduMap.setMyLocationEnabled(true);

     mCurrentMode = LocationMode.NORMAL;
     mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
     mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
             mCurrentMode, true, mCurrentMarker));
     LocationClientOption option = new LocationClientOption();
     option.setOpenGps(true); //打开gps
     option.setCoorType("bd09ll"); // 设置坐标类型
     option.setScanSpan(1000);

   //设置定位完成后需要返回地址
        option.setIsNeedAddress(true);
        //设置定位完成后需要的定位描述
        option.setIsNeedLocationDescribe(true);
     mLocClient.setLocOption(option);
     mLocClient.start(); 


        mainList = (ListView) findViewById(R.id.mainlist);
        myView = (MySideBar) findViewById(R.id.myview);
        tv01 = (TextView) findViewById(R.id.main_tv01);
        myView.setOnTouchingLetterChangedListener(this);
        data = new ArrayList<String>();
        letterCharList = new ArrayList<Integer>();
        letterPositionList = new ArrayList<Integer>();

        int index = 0, position = 0;
        letterCharList.add(index);
        for (int i = 0; i < ary.length; i++) {
                        for (int j = 0; j < ary[i].length; j++) {
                            if (i == 0 && j == 0) {
                                index++;
                                letterPositionList.add(position);
                        } else if (j == 0) {
                                letterCharList.add(index);
                                letterPositionList.add(position);
                                index++;
                            } else {
                                letterCharList.add(-1);
                            }
                            position++;
                            data.add(ary[i][j]);
                        }
                    }
        MyAdapter adapter = new MyAdapter(this, data, letterCharList, title);
        mainList.setAdapter(adapter);
        mainList.setOnScrollListener(new OnScrollListener() {

            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (letterCharList.get(firstVisibleItem) >= 0) {
//                  tv01.setText(title[letterCharList.get(firstVisibleItem)]);
                    tv01.setText("当前城市"+city);//设置值
                    lastFirstVisibleItem = firstVisibleItem;
                } else {    
                    if (lastFirstVisibleItem > firstVisibleItem) {
//                      tv01.setText(title[letterCharList
//                              .get(lastFirstVisibleItem) - 1]);
                        tv01.setText("当前城市"+city);//设置值
                    }
                }
            }
        });

        //点击item项,转换成要定位的城市
        mainList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

//          Toast.makeText(getApplicationContext(), mainList.getItemAtPosition(position)+"我被点击了。。。", 0).show();
            Toast.makeText(getApplicationContext(), "当前城市"+mainList.getItemAtPosition(position),0).show();
            tv01.setText((CharSequence) mainList.getItemAtPosition(position));//将城市定位的位置换成点击的城市
        }
    });
    }


    public void onTouchingLetterChanged(int s) {
        mainList.setSelection(letterPositionList.get(s));
    }

    /** 数据源二维数组 */
    private String[][] ary = {

//          {"正在定位..."},
            // 热门城市
            { "北京", "上海", "石家庄", "昆明", "杭州", "广州", "成都", "西安", "南京", "深圳",
                    "重庆", "长沙", "沈阳", "厦门", "武汉", "海口", "乌鲁木齐", "青岛", "大连",
                    "三亚", "哈尔滨", "长春", "南宁", "贵阳", "福州", "郑州", "济南", "桂林",
                    "天津", "太原", "南昌" },
            // 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

            { "重庆市", "张家口市", "长治市", "长春市", "镇江市", "舟山市", "漳州市", "淄博市", "枣庄市",
                    "郑州市", "周口市", "驻马店市", "株洲市", "张家界市", "珠海市", "肇庆市", "湛江市",
                    "中山市", "张掖市", "自贡市", "资阳市", "遵义市", "昭通市", "中卫市" } };
     public class MyLocationListenner implements BDLocationListener {

            @Override
            public void onReceiveLocation(BDLocation location) {
                // map view 销毁后不在处理新接收的位置
                if (location == null || mMapView == null) {
                    return;
                }
//              tv_map.setText("[我的位置]\n" + location.getStreet());
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                                // 此处设置开发者获取到的方向信息,顺时针0-360
                        .direction(100).latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();

                //定位当前城市   
                city=location.getCity();
                tv01.setText(city);//将当前定位的城市设置给textview
                mBaiduMap.setMyLocationData(locData);

                if (isFirstLoc) {
                    isFirstLoc = false;
                    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) {
            }
        }


     @Override
        protected void onDestroy() {
            super.onDestroy();
            // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
            mLocClient.stop();
            //定位层关闭
            mBaiduMap.setMyLocationEnabled(false);
        }

        @Override
        protected void onResume() {
            super.onResume();
            // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
            mMapView.onResume();
        }

        @Override
        protected void onPause() {
            super.onPause();
            // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
            mMapView.onPause();
        }

}
布局文件
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">
    <ListView
        android:id="@+id/mainlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"
        android:scrollbars="none"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dip"
        />

    <TextView
        android:id="@+id/main_tv01"
        android:layout_width="fill_parent"
        android:layout_height="30dp"

        android:gravity="center_vertical"
         android:background="#F0F0F0"
       android:textColor="#ff55bb22"
        android:textSize="20sp" 
        android:text="9823"
        android:paddingLeft="10dp"/>

    <com.example.test0504.MySideBar
        android:id="@+id/myview"
        android:layout_gravity="right"
        android:layout_width="30dip"
        android:layout_height="fill_parent"
        />        

    <com.baidu.mapapi.map.MapView  
        android:id="@+id/bmapView"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:visibility="gone"
        android:clickable="true" /> 

 </FrameLayout>

自定义view
package com.example.test0504;

import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;

 public class MySideBar extends View{
    OnTouchingLetterChangedListener onTouchingLetterChangedListener;
 // 按住改变背景色
    private boolean showBkg;
    public static String[] b = { "定","热", "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" };
    /**被选中位置*/
    int choose = -1;
    private Paint paint = new Paint();


    public MySideBar(Context context) {
        super(context);
    }

    public MySideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MySideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(showBkg){
            canvas.drawColor(Color.parseColor("#ccddFF"));
        }

        float height = getHeight();
        float width = getWidth();
 //     计算单个字母高度
        float singleHeight = height / (float)(b.length);

        for(int i = 0; i < b.length; i++ ){
            paint.setColor(Color.BLACK);
            paint.setTextSize(20);
            if (i == choose) {
 //             选中的颜色
                paint.setColor(Color.parseColor("#3399ff"));
 //             加粗
                paint.setFakeBoldText(true);
            }
 //         设置文本坐标
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;

            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();
        }
    }

    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        final int oldChoose = choose;
        final int c = (int) (y / getHeight() * b.length);
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            showBkg = true;
            if (oldChoose != c && listener != null) {
                if (c >= 0 && c < b.length) {
                    listener.onTouchingLetterChanged(c);
                    choose = c;
                    invalidate();
                }
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (oldChoose != c && listener != null) {
                if (c >= 0 && c < b.length) {
                    listener.onTouchingLetterChanged(c);
                    choose = c;
                    invalidate();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            showBkg = false;
            choose = -1;
            invalidate();
            break;
        }
        return true;
    }
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    public interface OnTouchingLetterChangedListener {
        public void onTouchingLetterChanged(int s);
    }
 }
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test0504"
    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" >
        <activity
            android:name=".CityActivity"
            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>

        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="ZVqLlEhHMY8LSzD4GpK8gezEuP0CCDgs" />


    </application>

</manifest>
布局文件
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

     <TextView
         android:id="@+id/mainlist_item_tv01"
         android:layout_width="fill_parent"
         android:layout_height="30dp"
         android:background="#F0F0F0"
         android:gravity="center_vertical"
         android:paddingLeft="10dp"
         android:textColor="#ff55bb22"
         android:text="test"
         android:textSize="20sp" />

     <TextView
         android:id="@+id/mainlist_item_tv02"
         android:layout_width="fill_parent"
         android:layout_height="40dp"
         android:gravity="center_vertical"
         android:paddingLeft="10dp"
         android:text="test"
         android:textSize="20dip" />

 </LinearLayout>

android使用自定控件实现城市列表展示并且实现当前城市定位

标签:

原文地址:http://blog.csdn.net/qq_33996621/article/details/51314927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!