定位操作

原创 2016年06月01日 14:37:29

activity_main.xml

<TextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text=" " />

 

mainactivity

 

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

    @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();
        }
       });
       tv.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
         // TODO Auto-generated method stub

         return false;
        }
       });
       
       
       
        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;
 }
}

AndroidManifest

 

<!-- 这个权限用于进行网络定位 -->

    <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"
            android:value="MvjkSNVUgwhN5fcws2iqHYqTI1P2DAG8" />

        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
        </service>

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

16.操作外设及重定位基本原理

16.1.操作某个外设的套路 16.2.设置栈和调用C语言 16.3.汇编程序和C程序互相调用 16.4.开启iCache 16.5.重定位和链接脚本引入 16.6.链接脚本详解 16.7.源码和bi...
  • Rston
  • Rston
  • 2016-12-21 22:42
  • 794

slw-定位操作说明书

  • 2013-08-18 20:49
  • 2.33MB
  • 下载

iPhone GPS定位系统 实例操作

iPhone GPS定位系统 实例操作是本文要介绍的内容,先来看本文详细内容。如今,配备GPS功能的移动设备越来越普遍了,使用GPS定位系统,可以精确地定位你当前所在的地理位置,但由于GPS接收机需要...
  • strliu
  • strliu
  • 2012-05-25 23:37
  • 1179

C例子:文件定位操作一

  • 2016-12-05 21:03
  • 1006B
  • 下载

C例子:文件定位操作二

  • 2016-12-06 21:46
  • 866B
  • 下载

百度地图定位操作

package com.example.baidumap; /*定位的基本操作 首先是 setMyLocationEnabled(true); 打开图层 然后是mLocatio...

OpenCV中鼠标定位操作

  • 2010-08-08 11:04
  • 16KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)