关闭

城市定位

73人阅读 评论(0) 收藏 举报

++++++++++++++++++++MainActivity


package com.bawei.map;


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

import com.baidu.location.Address;
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 android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements OnItemClickListener{
    
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();
    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;
    private ListView lv;
    private ListView lv1;
    private TextView tv;
    private Button location_place;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location_place=(Button)findViewById(R.id.location_place);

        tv = (TextView)findViewById(R.id.tv);

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

     // LocationClient类是定位SDK的核心类
             mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
             mLocationClient.registerLocationListener(myListener); // 注册监听函数
             location_place.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v) {
                     
                     initLocation();
                 }
             });
             
             tv.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    Intent intent=new Intent(MainActivity.this,NextActivity.class);
                    intent.putExtra("cityName", tv.getText().toString().trim());
                    startActivity(intent);
                }

            });

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

             
        
        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);
        
        

    }


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //定位
    protected 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 district = location.getDistrict();
            String street = location.getStreet();
            Address address = location.getAddress();
            Log.i("Main", address.toString());
            
            tv.setText(city + "---" + street+" "+district);
        }
    }


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


    class MyAdapter1 extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return letter.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View arg1, ViewGroup arg2) {
            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 arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
        
         @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 arg2) {
             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;
        }
        
    }
    
    

    



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        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;
        }
    
        
    }
    
    class ViewHolder{
        TextView tv;
        LinearLayout ll;
    }
    class ViewHolder1{
        TextView tv;
    }
    class ViewHolder2{
        TextView tv;
    }


}


Layout文件

====================================================================================================

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bawei.map.MainActivity" >

    <Button
        android:id="@+id/location_place"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GPS定位"
        />

    <TextView
        android:id="@+id/tv"
        android:layout_below="@id/location_place"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    
     <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv"
        android:scrollbars="none" >
    </ListView>

    <ListView
        android:id="@+id/listView2"
        android:layout_width="15dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        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>

===================================================================

   letter_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:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/letterTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>

=======================================================================

       litter_list

<?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/letterListTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textColor="#3aa2cf" />

</LinearLayout>

===========================================================================


                  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>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:580次
    • 积分:64
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档