城市菜单

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>
记得里面的服务要配置要不不能进行定位

时间: 2024-10-10 19:41:14

城市菜单的相关文章

Python 城市菜单详解(超详解)

print("--------城市查询系统---------") print("--------按数值进行查询--------") menu={"内蒙古":{"赤峰市":["赤峰1","赤峰2","赤峰3"],"锡盟":["锡盟1","锡盟2","锡盟3"]},       "上

Bootstrap关联select菜单,包罗万象的知识点

今天我将介绍自定义的bootstrap关联select菜单,主要应用场合有省市级关联菜单等等,那么就先拿这个例子来讲,当然其他场景的关联菜单也同样适用.说实话,封装好一个通用的组件还是需要花费很多精力的和时间的,所谓通用,自然要考虑周全,叹!这次整理的Bootstrap关联select,里面也涉及到了很多jquery.ajax.springMVC等等知识点,可谓包罗万象! 首先,请允许我代表该自定义组件做一番小小的介绍. "hi,你好,我叫yunm.combox.js,主人给我起的名字,其实呢,

JS省市县三级联动菜单

<html> <head> <title>JS省市县三级联动菜单丨潜水曝气机</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <style> body,select { font-size:9pt; font-family:Verdana; } a { color:red; text-dec

[ PHP+jQuery ] ajax 多级联动菜单的应用:电商网站的用户地址选择功能 ( 一 ) - 传统下拉菜单

/** jQuery version: 1.8.3 Author: 小dee Date: 2014.11.5 */ 说明:分析其他网站的图片较多,可以在目录跳过直接看本文 demo . 目录: 其他网站分析 亚马逊 淘宝 京东 当当 本文案例 demo1 demo2 [ 后面的博文再写 ] demo3 [ 后面的博文再写 ] 惯例,先看看他山之石,选择了四家比较大的电商网站:亚马逊.淘宝.京东.当当,看看它们的地址联动菜单是怎么做的. 1. 亚马逊[返回目录][下一节:淘宝] 图1 默认界面 说

学习级联函数的心得

昨天看了某学院讲级联函数的视频,没看明白,在网上找了个级联函数实例来琢麽,是一个省会和城市级联的下拉菜单列表. html和js如下: <html>     <head>     <title>省份与省市级联</title>     </head> <script language="javascript" src="province.js" type="text/javascript&quo

转: javascript实现全国城市三级联动菜单代码

<html> <head> <title>js全国城市三级联动菜单代码_B5教程网</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <script LANGUAGE="JavaScript"> function Dsy() { this.Items = {}; } Dsy

python学习之路之案例3--多级菜单显示城市详细信息

一.整个案例运用到的知识点 1.python数据结构之字典的使用,字典嵌套字典,字典嵌套列表 2.python数据结构之列表的使用,字典嵌套列表 3.python数据结构之字符串的使用,字符串的格式化 4.while True死循环的使用 5.if...else....语句的使用 二.案例设计核心思想 1.将城市信息存储在字典+列表的数据结构里面里面 2.将省.市.先设置成三级菜单 3.按照用户的选择可依次选择进入各子菜单 三.代码 #!/usr/bin/env python # -*- cod

JS实战 &#183; 级联菜单选择省份和城市(两种)

DOM编程步骤.思路 1.定义界面: 通过html标签将数据进行封装: 2.定义一些静态样式: 利用css: 3.需要动态地完成的和用户的交互: a:明确事件源: b:明确事件,将事件注册到事件源上: c:通过JS的函数对象对事件进行封装: d:在处理过程中需要明确处理区域. 代码一如下: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta htt

作业2:城市三级菜单

三级菜单: 1. 运行程序输出第一级菜单 2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单 3. 菜单数据保存在文件中 4. 让用户选择是否要退出 5. 有返回上一级菜单的功能 flag=Truedict_menu={#定义城市字典名称. "四川省":{ "成都市":["新都区","成华区","天府新区"], }, "山东省": { "青岛市": ["