利用百度地图定位城市
来源:互联网 发布:万能视频下载 mac 编辑:程序博客网 时间:2024/05/07 02:59
先上图
接下来,我们就要实现这样一个效果:
下面贴出主要的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#F3F3F3"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/layhead"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#bc1d1b" >
<ImageView
android:id="@+id/imgback"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/imgback" />
<TextView
style="@style/ActionBar_title_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="选择城市"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/search_lay"
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_below="@+id/layhead"
android:padding="10dip" >
<EditText
android:id="@+id/sh"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/searchedit"
android:drawableLeft="@drawable/ic_search"
android:drawablePadding="10dp"
android:padding="10dip"
android:hint="请输入城市名称"
android:textColor="#515151"
android:textSize="13dp" >
</EditText>
</RelativeLayout>
<RelativeLayout
android:id="@+id/gps_city_lay"
android:layout_below="@+id/search_lay"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:background="#B7B7B7"
android:paddingBottom="2dip"
android:paddingLeft="13dip"
android:paddingTop="2dip"
android:textColor="#515151"
android:textSize="14sp"
android:visibility="gone" />
<LinearLayout
android:id="@+id/lng_city_lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/alpha"
android:background="@drawable/wireframe"
android:padding="10dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:background="@drawable/ic_citylist_gps" />
<TextView
android:id="@+id/lng_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip"
android:text="正在定位所在城市.."
android:textColor="#515151" />
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/gps_city_lay"
android:cacheColorHint="#00000000"
android:scrollbars="none" />
<com.winxiang.locationselect.MyLetterListView
android:id="@+id/MyLetterListView01"
android:layout_width="30dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/gps_city_lay" />
</RelativeLayout>
这是主要的布局文件
private void initGps() {
try{
MyLocationListenner myListener = new MyLocationListenner();
locationClient = new LocationClient(ActivitySelectCity.this);
locationClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("all");
option.setCoorType("bd09ll");
option.setScanSpan(5000);
option.disableCache(true);
option.setPoiNumber(5);
option.setPoiDistance(1000);
option.setPoiExtraInfo(true);
option.setPriority(LocationClientOption.GpsFirst);
locationClient.setLocOption(option);
locationClient.start();
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
locationClient.stop();
}
private class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
StringBuffer sb = new StringBuffer(256);
if (location.getLocType() == BDLocation.TypeGpsLocation) {
// sb.append(location.getAddrStr());
sb.append(location.getCity());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append(location.getCity());
}
if (sb.toString() != null && sb.toString().length() > 0) {
lngCityName=sb.toString();
lng_city.setText(lngCityName);
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
上面代码是百度地图的相关代码
下面主要来实现字母排序的相关代码,这里我们需要自定义控件
public class MyLetterListView extends View {
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
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;
Paint paint = new Paint();
boolean showBkg = false;
public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyLetterListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLetterListView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (showBkg) {
canvas.drawColor(Color.parseColor("#10000000"));
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i++) {
paint.setColor(Color.parseColor("#515151"));
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
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();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if (oldChoose != c && listener != null) {
if (c >= 0 && c <= b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null) {
if (c >= 0 && c <= b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}
adapter的实现
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
final int VIEW_TYPE = 3;
public ListAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
alphaIndexer = new HashMap<String, Integer>();
sections = new String[ShowCity_lists.size()];
for (int i = 0; i < ShowCity_lists.size(); i++) {
// 当前汉语拼音首字母
String currentStr = getAlpha(ShowCity_lists.get(i).getPinyi());
// 上一个汉语拼音首字母,如果不存在为“ ”
String previewStr = (i - 1) >= 0 ? getAlpha(ShowCity_lists.get(i - 1)
.getPinyi()) : " ";
if (!previewStr.equals(currentStr)) {
String name = getAlpha(ShowCity_lists.get(i).getPinyi());
alphaIndexer.put(name, i);
sections[i] = name;
}
}
}
@Override
public int getCount() {
return ShowCity_lists.size();
}
@Override
public Object getItem(int position) {
return ShowCity_lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
int type = 2;
if (position == 0&&sh.getText().length()==0) {//不是在搜索状态下
type = 0;
}
return type;
}
@Override
public int getViewTypeCount() {// 这里需要返回需要集中布局类型,总大小为类型的种数的下标
return VIEW_TYPE;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.alpha = (TextView) convertView
.findViewById(R.id.alpha);
holder.name = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// if (sh.getText().length()==0) {//搜所状态
// holder.name.setText(list.get(position).getName());
// holder.alpha.setVisibility(View.GONE);
// }else if(position>0){
//显示拼音和热门城市,一次检查本次拼音和上一个字的拼音,如果一样则不显示,如果不一样则显示
holder.name.setText(ShowCity_lists.get(position).getName());
String currentStr = getAlpha(ShowCity_lists.get(position).getPinyi());//本次拼音
String previewStr = (position-1) >= 0 ? getAlpha(ShowCity_lists.get(position-1).getPinyi()) : " ";//上一个拼音
if (!previewStr.equals(currentStr)) {//不一样则显示
holder.alpha.setVisibility(View.VISIBLE);
if (currentStr.equals("#")) {
currentStr = "热门城市";
}
holder.alpha.setText(currentStr);
} else {
holder.alpha.setVisibility(View.GONE);
}
// }
return convertView;
}
private class ViewHolder {
TextView alpha; // 首字母标题
TextView name; // 城市名字
}
}
好了主要代码就是这些
- 利用百度地图定位城市
- 百度地图定位城市
- 百度地图的城市定位
- 通过百度地图定位城市
- 百度地图定位当前城市
- 利用百度地图定位
- 城市定位,利用百度地图API定位到你当前的位置
- 百度地图 定位到当前城市
- 百度地图api定位到当前城市
- 百度地图SDK 网络定位城市名称
- Android小项目之利用百度地图定位获取当前城市
- 微信小程序城市定位(借助百度地图API判断城市)
- 利用百度SDK进行城市定位
- 百度地图API的IP定位城市和浏览器定位
- 百度地图定位当前城市并设置中心点
- 网页版百度地图--根据城市名定位
- html5 geolocation / 百度地图api Geolocation 定位当前城市信息
- html5 geolocation/ 百度地图api Geolocation 定位当前城市信息
- python unittest源码解析二----Test Discovery
- php实例-对象与类
- DbHelperSQL类
- 【C++】String_COW(写时拷贝)
- 【UI初级 连载二】------做一个100秒倒计时的程序,注意考虑,当程序进入后台时的情况。
- 利用百度地图定位城市
- protobuf安装与示例
- 新博客(Hexo)地址:http://leoios.github.io
- 新建远端分支,git branch & git remote & origin master
- C++primer 阅读笔记-模板与泛型编程(模板参数)
- 为什么产品思考是用户体验设计时所不可或缺的?
- CacheHelper类
- 为什么eval某个json字符串时要加括号?
- 仿微信联系人分组列表