百度地图实现定位功能及城市选择
由于项目需要于是单独抽出了一点时间实现了这个功能,目前效果还不错,应该可以满足大多数的需求。下面图片是UI效果图:
1,实现该效果首先需要集成百度地图sdk ,主要集成基础定位,室内定位,基础地图,检索功能,计算工具等模块sdk。具体的集成方式参考此处点击打开链接
2,百度地图集成成功之后就可以开始地图的开发了
地图模块
首先是布局的构建,新建布局文件fragment_shopstore.xml
<LinearLayout 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:background="@color/white"
android:orientation="vertical"
tools:context="com.first.mzystore.home.fragment.ClassifyFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_poi_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="杭州"
android:layout_alignParentLeft="true"
android:textSize="@dimen/text_size_14"
android:layout_centerVertical="true"
android:textColor="@color/c_333333"
android:drawableLeft="@mipmap/location"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近门店"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:textSize="@dimen/text_size_title"
android:textColor="@color/c_333333"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看附近门店"
android:layout_alignParentRight="true"
android:textSize="@dimen/text_size_14"
android:layout_centerVertical="true"
android:textColor="@color/blue" />
</RelativeLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/mmap"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.baidu.mapapi.map.MapView>
</LinearLayout>
接下来就是控件的初始化:
GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
// 定位相关
LocationClient mLocClient;
//定位监听
public MyLocationListenner myListener = new MyLocationListenner();
mMapView = (MapView) view.findViewById(R.id.mmap);
tvSearch = view.findViewById(R.id.tv_search);
tvPoiCity = view.findViewById(R.id.tv_poi_city);
}
//初始化 地图信息
mBaiduMap = mMapView.getMap();
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
mBaiduMap.setBuildingsEnabled(false);//设置是否展示3d效果
mMapView.showZoomControls(false);
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(listener);
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
mlocation = location;
if(location.getCity()!=null) {
cityName = location.getCity().replace("市", "");
}
myCityName = cityName;
Log.w("--------",location.getAddrStr());
MyLocationData locData = new MyLocationData.Builder()
.accuracy(mlocation.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(mlocation.getLatitude())
.longitude(mlocation.getLongitude()).build();
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(13.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
setLocation();
}
if (mLocClient.isStarted()) {
// 获得位置之后停止定位
mLocClient.stop();
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
//选择其他城市需要用到百度地图的反编码功能 实现地理编码检索回调
// 创建地理编码检索
OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
public void onGetGeoCodeResult(GeoCodeResult result) {
//编码检索 根据传入的城市和详细地址 可以定位到某个城市
Log.w("----ERRORNO---",result.error+"");
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getActivity(), "抱歉,未能找到结果", Toast.LENGTH_LONG)
.show();
return;
}
// 下面是添加检索结果的marker点 可以根据需要自己选是否显示
// mBaiduMap.clear();
// mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
// .icon(BitmapDescriptorFactory
// .fromResource(R.mipmap.icon_marka)));
// mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
// .getLocation()));
//获取地理编码结果
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
//反编码检索 根据传入的经纬度 可以定位到某个城市
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getActivity(), "抱歉,未能找到结果", Toast.LENGTH_LONG)
.show();
return;
}
// 下面是添加检索结果的marker点 可以根据需要自己选是否显示
// mBaiduMap.clear();
// mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
// .icon(BitmapDescriptorFactory
// .fromResource(R.mipmap.icon_marka)));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
if(cityName.equals("上海")) {
setNewLocation(list2);
}else if(cityName.equals("北京")){
setNewLocation(list3);
}
}
};
在城市选择之后回到地图页面时调用下面方法:
需要传入城市的经纬度,后续会附上相关json文件
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latLng).newVersion(1));
地图页面完整代码
package com.first.mzystore.home.fragment;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.SpannableString;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
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.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.utils.DistanceUtil;
import com.first.mzystore.R;
import com.first.mzystore.base.BaseFragment;
import com.first.mzystore.home.activity.groupactivity.UpgradeActivity;
import com.first.mzystore.home.activity.mineactivity.myorder.GoodsCommentActivity;
import com.first.mzystore.home.activity.shopmap.poi.ShopPoiActivity;
import com.first.mzystore.home.adapter.ShopMapListAdapter;
import com.first.mzystore.home.bean.CityListBean;
import com.first.mzystore.utils.FileUtil;
import com.first.mzystore.utils.MhomeUtil;
import com.first.mzystore.utils.TakePhotoUtil;
import com.yxp.permission.util.lib.PermissionUtil;
import com.yxp.permission.util.lib.callback.PermissionResultCallBack;
import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class ShopDoorFragment extends BaseFragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private MapView mMapView;
private BaiduMap mBaiduMap;
private String mParam1;
private String mParam2;
private String cityName = "";
private String myCityName = "";
private LatLng latLng;
GeoCoder mSearch = null; // 搜索模块,也可去掉地图模块独立使用
private ArrayList<LatLng> list = new ArrayList<>();
private ArrayList<LatLng> list2 = new ArrayList<>();
private ArrayList<LatLng> list3 = new ArrayList<>();
private ArrayList<String> distanceList = new ArrayList<>();
private ArrayList<String> areaList = new ArrayList<>();
private InfoWindow mInfoWindow;
private ArrayList<String> mapList = new ArrayList<>();
// 定位相关
LocationClient mLocClient;
boolean isFirstLoc = true; // 是否首次定位
BDLocation mlocation;
//定位监听
public MyLocationListenner myListener = new MyLocationListenner();
//自定义精度圈填充颜色
private int accuracyCircleFillColor = 0xAAFFFF88;//自定义精度圈填充颜色
//自定义精度圈边框颜色
private int accuracyCircleStrokeColor = 0xAA00FF00;//自定义精度圈边框颜色
LatLng poiLatlng = null;
private LocationMode mCurrentMode = LocationMode.FOLLOWING;//定位跟随态
// private LocationMode mCurrentMode = LocationMode.NORMAL; //默认为 LocationMode.NORMAL 普通态
// private LocationMode mCurrentMode = LocationMode.COMPASS; //定位罗盘态
private TextView tvSearch,tvPoiCity;
private ArrayList<CityListBean> cityList = new ArrayList<>();
public ShopDoorFragment() {
// Required empty public constructor
}
public static ShopDoorFragment newInstance(String param1, String param2) {
ShopDoorFragment fragment = new ShopDoorFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
protected void findViews(View view) {
mMapView = (MapView) view.findViewById(R.id.mmap);
mBaiduMap = mMapView.getMap();
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
mBaiduMap.setBuildingsEnabled(false);//设置是否展示3d效果
mMapView.showZoomControls(false);
tvSearch = view.findViewById(R.id.tv_search);
tvPoiCity = view.findViewById(R.id.tv_poi_city);
// 初始化搜索模块,注册事件监听
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(listener);
}
@Override
protected void initViews() {
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
setLocClient();
PermissionUtil.getInstance().request(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
new PermissionResultCallBack() {
@Override
public void onPermissionGranted() {
// 当所有权限的申请被用户同意之后,该方法会被调用
mLocClient.start();
}
@Override
public void onPermissionGranted(String... strings) {
// 当所有权限的申请被用户同意之后,该方法会被调用
}
@Override
public void onPermissionDenied(String... permissions) {
Toast.makeText(getActivity(),"部分权限被禁止需要手动开启",Toast.LENGTH_SHORT).show();
// 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出时,该方法将会被调用
}
@Override
public void onRationalShow(String... permissions) {
Toast.makeText(getContext(),"部分权限被禁止,可能会影响定位的精确性",Toast.LENGTH_SHORT).show();
// 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后,该方法将会被调用.
}
});
tvPoiCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ShopPoiActivity.class);
intent.putExtra("cityName",cityName);
intent.putExtra("myCityName",myCityName);
startActivityForResult(intent,1);
}
});
}
public void setLocClient(){
// 定位初始化
mLocClient = new LocationClient(getActivity());
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置定位坐标类型
option.setScanSpan(10000);////设置定位扫描间隔时间
option.setAddrType("all");
option.setIsNeedLocationDescribe(true);//设置是否需要位置语义化结果
mLocClient.setLocOption(option);
}
public LatLng getCityLat(String cityNames){
for(int i=0;i<cityList.size();i++){
if(cityList.get(i).getCityName().equals(cityNames)){
latLng = new LatLng(Double.valueOf(cityList.get(i).getLat()),Double.valueOf(cityList.get(i).getLog()));
break;
}
}
return latLng;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode){
case 100:
cityName = data.getStringExtra("cityName");
tvPoiCity.setText(cityName);
if(cityName.equals(myCityName)){
isFirstLoc = true;
if(mLocClient !=null) {
mLocClient.restart();
}
}else{
latLng = new LatLng(Double.valueOf(data.getStringExtra("lat")),Double.valueOf(data.getStringExtra("log")));
if(mLocClient !=null && mLocClient.isStarted()) {
mLocClient.stop();
}
reLocation();
}
break;
}
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
mlocation = location;
if(location.getCity()!=null) {
cityName = location.getCity().replace("市", "");
}
myCityName = cityName;
Log.w("--------",location.getAddrStr());
MyLocationData locData = new MyLocationData.Builder()
.accuracy(mlocation.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(mlocation.getLatitude())
.longitude(mlocation.getLongitude()).build();
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(13.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
setLocation();
}
// LatLng p1LL = new LatLng(location.getLatitude(),
// location.getLongitude());
// LatLng p2LL = new LatLng(list.get(3).latitude,list.get(3).longitude);
// double distance = DistanceUtil.getDistance(p1LL, p2LL);
//
// //转换成公里
// String temp;
// if (distance < 1000) {
// temp = distance+"米"; //距离误差大小为500米以内
// }else{
// temp = String.format("%.2f",(distance)/1000)+"km";
// }
// Log.w("--------distance=",distance+"------temp---"+temp);
// // 根据BDLocation 对象获得经纬度以及详细地址信息
// double latitude = location.getLatitude();
// double longitude = location.getLongitude();
// String address = location.getAddrStr();
// // 将GPS设备采集的原始GPS坐标转换成百度坐标
// poiLatlng = new LatLng(latitude,longitude);
// CoordinateConverter converter = new CoordinateConverter();
// converter.from(CoordinateConverter.CoordType.GPS);
// // sourceLatLng待转换坐标
// converter.coord(poiLatlng);
// LatLng desLatLng = converter.convert();
// //设定中心点坐标
// //LatLng cenpt = new LatLng(29.806651,121.606983);
// //定义地图状态
// MapStatus mMapStatus = new MapStatus.Builder()
// .target(ll)
// .zoom(13.0f)
// .build();
// //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
// MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
// //改变地图状态
// mBaiduMap.setMapStatus(mMapStatusUpdate);
// //setLocation();
if (mLocClient.isStarted()) {
// 获得位置之后停止定位
mLocClient.stop();
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
public void reLocation(){
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latLng).newVersion(1));
}
// 创建地理编码检索
OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
public void onGetGeoCodeResult(GeoCodeResult result) {
Log.w("----ERRORNO---",result.error+"");
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getActivity(), "抱歉,未能找到结果", Toast.LENGTH_LONG)
.show();
return;
}
mBaiduMap.clear();
// mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
// .icon(BitmapDescriptorFactory
// .fromResource(R.mipmap.icon_marka)));
// mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
// .getLocation()));
Log.w("----1111---",result.getAddress()+"");
//获取地理编码结果
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getActivity(), "抱歉,未能找到结果", Toast.LENGTH_LONG)
.show();
return;
}
mBaiduMap.clear();
// mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
// .icon(BitmapDescriptorFactory
// .fromResource(R.mipmap.icon_marka)));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
if(cityName.equals("上海")) {
setNewLocation(list2);
}else if(cityName.equals("北京")){
setNewLocation(list3);
}
// Log.w("----1111---",result.getAddress()+"");
//获取反向地理编码结果
}
};
public void setLocation(){
mBaiduMap.clear();
for(int i=0;i<list.size();i++) {
// LatLng lats = new LatLng(mapList.get(i).getLng(), mapList.get(i).getLat());
LatLng p1LL = new LatLng(mlocation.getLatitude(),
mlocation.getLongitude());
LatLng p2LL = new LatLng(list.get(i).latitude,list.get(i).longitude);
double distance = DistanceUtil.getDistance(p1LL, p2LL);
//转换成公里
String temp;
// if (distance < 1000) {
// temp = distance+"米"; //距离误差大小为500米以内
// }else{
temp = String.format("%.1f",(distance)/1000)+"km";
// }
distanceList.add(temp);
LatLng lats = list.get(i);
// 得到一个标记的控制器
MarkerOptions mMarkerOptions = new MarkerOptions();
// 我们设置标记的时候需要传入的参数
BitmapDescriptor mbitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.mipmap.location);
// 设置标记的图标
mMarkerOptions.icon(mbitmapDescriptor);
// 设置标记的坐标
mMarkerOptions.position(lats);
mMarkerOptions.title("餐馆"+i);//mapList.get(i).getShopName()
// 添加标记
mBaiduMap.addOverlay(mMarkerOptions);
}
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mBaiduMap.hideInfoWindow();
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
});
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
showLocation(marker);
return true;
}
});
}
public void setNewLocation(ArrayList<LatLng> list){
for(int i=0;i<list.size();i++) {
LatLng lats = list.get(i);
// 得到一个标记的控制器
MarkerOptions mMarkerOptions = new MarkerOptions();
// 我们设置标记的时候需要传入的参数
BitmapDescriptor mbitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.mipmap.location);
// 设置标记的图标
mMarkerOptions.icon(mbitmapDescriptor);
// 设置标记的坐标
mMarkerOptions.position(lats);
mMarkerOptions.title("");//mapList.get(i).getShopName()
// 添加标记
mBaiduMap.addOverlay(mMarkerOptions);
}
mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mBaiduMap.hideInfoWindow();
}
@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
});
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
showLocation(marker);
return true;
}
});
}
private void showLocation(final Marker marker) { //显示气泡
LatLng pt = null;
double latitude, longitude;
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
InfoWindow infoWindow;
TextView tv = new TextView(getActivity());
tv.setBackgroundResource(R.drawable.change_btn_bg_gray);
tv.setPadding(30, 20, 30, 20);// 大小
SpannableString msp = new SpannableString("正常字体字体大小一半\n色正常粗体斜体粗斜");
tv.setText(msp);
tv.setTextColor(Color.parseColor("#000000"));
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showShopPopupWindow(view);
mBaiduMap.hideInfoWindow();
}
});
final LatLng latLng = marker.getPosition();
Point p = mBaiduMap.getProjection().toScreenLocation(latLng);
p.y -= 47;
LatLng ll = mBaiduMap.getProjection().fromScreenLocation(p);
mInfoWindow = new InfoWindow(tv, ll, 1);
mBaiduMap.showInfoWindow(mInfoWindow); //显示气泡
}
@Override
public void onPause() {
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
super.onPause();
}
@Override
public void onResume() {
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
super.onResume();
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
// System.out.println("不可见");
if (mLocClient !=null && mLocClient.isStarted()) {
mLocClient.stop();
}
} else {
// System.out.println("当前可见");
if(mLocClient !=null ) {
if(cityName.equals(myCityName)||cityName.equals("")) {
mLocClient.restart();
}
}else{
setLocClient();
if(cityName.equals(myCityName)) {
mLocClient.start();
}
}
}
}
@Override
public void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
if(mSearch!=null){
// 释放地理编码检索实例
mSearch.destroy();
mSearch=null;
}
super.onDestroy();
}
@Override
protected void initData() {
list.add(new LatLng(30.26754952473479,120.2128132037831));
list.add(new LatLng(30.278347949004072,120.24241913278202));
list.add(new LatLng(30.251947460259043,120.21537499846788));
list.add(new LatLng(30.267093781384556,120.22008588123839));
list2.add(new LatLng(31.24555,121.506258));
list2.add(new LatLng(31.245179,121.508557));
list2.add(new LatLng(31.243967,121.51286));
list3.add(new LatLng(39.925338,116.394783));
list3.add(new LatLng(39.935851,116.420654));
list3.add(new LatLng(39.938395,116.315444));
mapList.clear();
if(isAvilible(getActivity(),"com.baidu.BaiduMap")) {//传入指定应用包名
mapList.add("百度地图");
}
if (isAvilible(getActivity(), "com.autonavi.minimap")) {
mapList.add("高德地图");
}
}
@Override
protected int getLayoutId() {
return R.layout.fragment_shopstore;
}
@Override
protected void initView() {
initData();
initViews();
}
private void showShopPopupWindow(View view) {
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(getActivity()).inflate(
R.layout.popupwindow_shoplist, null);
ListView listView = contentView.findViewById(R.id.lv_shop);
final PopupWindow popupWindow = new PopupWindow(contentView,
WindowManager.LayoutParams.MATCH_PARENT, MhomeUtil.getScreenHeight(getActivity())*1/3, true);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
listView.setAdapter(new ShopMapListAdapter(mapList,getActivity()));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(mapList.get(i).equals("百度地图")){
isBaiDuHave();
}else if(mapList.get(i).equals("高德地图")){
isGaoDeHave();
}
popupWindow.dismiss();
// 设置背景颜色变暗
TakePhotoUtil.setBackgroundAlpha(getActivity(),1.0f);
}
});
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
TakePhotoUtil.setBackgroundAlpha(getActivity(),1.0f);
}
});
// 设置背景颜色变暗
TakePhotoUtil.setBackgroundAlpha(getActivity(),0.7f);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.color.white));
// 设置好参数之后再show
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
/** 检查手机上是否安装了指定的软件
* @param context
* @param packageName:应用包名
* @return*/
public static boolean isAvilible(Context context, String packageName){
//获取packagemanager
final PackageManager packageManager = context.getPackageManager();
//获取所有已安装程序的包信息
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
//用于存储所有已安装程序的包名
List<String> packageNames = new ArrayList<String>();
//从pinfo中将包名字逐一取出,压入pName list中
if(packageInfos != null){
for(int i = 0; i < packageInfos.size(); i++){
String packName = packageInfos.get(i).packageName;
packageNames.add(packName);
}
}
//判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE
return packageNames.contains(packageName);
}
public void isBaiDuHave(){
Intent intent = null;
if(isAvilible(getActivity(),"com.baidu.BaiduMap")){//传入指定应用包名
try {
intent = Intent.getIntent("baidumap://map/direction?" +
"destination="+list.get(3).latitude+","+list.get(3).longitude+"&index=0&target=1");
startActivity(intent); //启动调用
} catch (URISyntaxException e) {
Log.e("intent", e.getMessage());
}
}else{//未安装
//market为路径,id为包名
//显示手机上所有的market商店
Toast.makeText(getActivity(), "您尚未安装百度地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
public void isGaoDeHave(){
Intent intent = null;
if (isAvilible(getActivity(), "com.autonavi.minimap")) {
try{
//intent = Intent.getIntent("amapuri://route/plan/?sourceApplication=版&did="+list.get(3).latitude+"&dlon="+list.get(3).longitude+"&dname=B&dev=0&t=0");
//intent = Intent.getIntent("amapuri://route/plan/?sid=S1&slat="+mlocation.getLatitude()+"&slon="+mlocation.getLongitude()+"&sname=我的位置&did=S2&dlat"+list.get(2).latitude+"&dlon="+list.get(2).longitude+"&dname=终点&dev=0&t=0");
intent = Intent.getIntent("amapuri://route/plan/?sid=S1&slat="+mlocation.getLatitude()+"&slon="+mlocation.getLongitude()+"&sname=我的位置&did=S2&dlat="+list.get(3).latitude+"&dlon="+list.get(3).longitude+"&dname=终点&dev=0&t=0");
//intent = Intent.getIntent("androidamap://viewMap?sourceApplication=版&dlat="+list.get(3).latitude+"&dlon="+list.get(3).longitude+"&dev=0&t=0");
startActivity(intent);
} catch (URISyntaxException e)
{e.printStackTrace(); }
}else{
Toast.makeText(getActivity(), "您尚未安装高德地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
}
城市选择页面
布局文件activity_shop_poi.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.first.mzystore.home.activity.shopmap.poi.utils.ClearEditText
android:id="@+id/filter_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@mipmap/city_edit"
android:drawableLeft="@drawable/yuanjiao_radus_10_gray"
android:hint="请输入城市名称"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textSize="15dp" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/country_lvcountry"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:dividerHeight="1px"
android:scrollbars="none"
android:divider="@color/listview_driver" />
<TextView
android:id="@+id/dialog"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@color/red"
android:gravity="center"
android:textColor="#ffffffff"
android:textSize="30dp"
android:visibility="invisible" />
<com.first.mzystore.home.activity.shopmap.poi.widget.SideBar
android:id="@+id/sidrbar"
android:layout_width="30dp"
android:layout_height="fill_parent"
android:layout_gravity="right|center" />
</FrameLayout>
</LinearLayout>
城市选择页面完整代码package com.first.mzystore.home.activity.shopmap.poi;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.first.mzystore.R;
import com.first.mzystore.base.BaseActivity;
import com.first.mzystore.home.activity.shopmap.poi.adapter.SortAdapter;
import com.first.mzystore.home.activity.shopmap.poi.bean.CityNameInfo;
import com.first.mzystore.home.activity.shopmap.poi.utils.CharacterParser;
import com.first.mzystore.home.activity.shopmap.poi.utils.ClearEditText;
import com.first.mzystore.home.activity.shopmap.poi.utils.PinyinComparator;
import com.first.mzystore.home.activity.shopmap.poi.widget.SideBar;
import com.first.mzystore.home.bean.CityListBean;
import com.first.mzystore.utils.FileUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ShopPoiActivity extends BaseActivity {
private TextView tvCurrent;
private CharacterParser characterParser;
private PinyinComparator pinyinComparator;
private SideBar sideBar;
private TextView dialog;
private ListView sortListView;
private SortAdapter adapter;
private String cityName="";
private ClearEditText mClearEditText;
private List<CityListBean> cityList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop_poi);
initData();
initView();
}
@Override
protected void findViews() {
}
@Override
protected void initViews(Bundle savedInstanceState) {
}
@Override
protected void initData(Bundle savedInstanceState) {
}
@Override
protected String initTitleCenterString() {
return TextUtils.isEmpty(getIntent().getStringExtra("cityName")) ? "选择城市" : "当前城市-"+getIntent().getStringExtra("cityName");
}
@Override
protected View initTitleRightButton() {
return null;
}
private void initData() {
cityName = getIntent().getStringExtra("myCityName");
String city_str = FileUtil.readAssets(this, "cityjson.json");
JSONArray cityArray = null;
try {
cityArray = new JSONArray(city_str);
for(int j=0;j<cityArray.length();j++){
CityListBean cityListBeans = new CityListBean();
JSONObject objects = cityArray.getJSONObject(j);
cityListBeans.setCityName(objects.getString("cityName"));
cityListBeans.setLog(objects.getString("log"));
cityListBeans.setLat(objects.getString("lat"));
cityList.add(cityListBeans);
}
} catch (JSONException e) {
e.printStackTrace();
}
// 实例化汉字转拼音类
characterParser = CharacterParser.getInstance();
pinyinComparator = new PinyinComparator();
filledData(cityList);
}
private void initView() {
View view = View.inflate(this, R.layout.head_city_list, null);
tvCurrent = view.findViewById(R.id.tv_current);
tvCurrent.setText(cityName);
sideBar = (SideBar) findViewById(R.id.sidrbar);
dialog = (TextView) findViewById(R.id.dialog);
sideBar.setTextView(dialog);
// 设置右侧触摸监听
sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
@Override
public void onTouchingLetterChanged(String s) {
// 该字母首次出现的位置
int position = adapter.getPositionForSection(s.charAt(0));
if (position != -1) {
sortListView.setSelection(position);
}
}
});
sortListView = (ListView) findViewById(R.id.country_lvcountry);
sortListView.addHeaderView(view);
sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// 这里要利用adapter.getItem(position)来获取当前position所对应的对象
if(position==0){
Intent data = new Intent();
data.putExtra("cityName",cityName);
setResult(100, data);
finish();
}else {
// Toast.makeText(getApplication(), ((CityListBean) adapter.getItem(position - 1)).getCityName(),
// Toast.LENGTH_SHORT).show();
Intent data = new Intent();
data.putExtra("lat", ((CityListBean) adapter.getItem(position - 1)).getLat());
data.putExtra("cityName", ((CityListBean) adapter.getItem(position - 1)).getCityName());
data.putExtra("log", ((CityListBean) adapter.getItem(position - 1)).getLog());
setResult(100, data);
finish();
}
}
});
// 根据a-z进行排序源数据
Collections.sort(cityList, pinyinComparator);
adapter = new SortAdapter(this, cityList);
sortListView.setAdapter(adapter);
mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
mClearEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
filterData(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
/**
* 为ListView填充数据
*
* @param date
* @return
*/
private void filledData(List<CityListBean> date) {
for (int i = 0; i < date.size(); i++) {
CityListBean sortModel = date.get(i);
// 汉字转换成拼音
String pinyin = characterParser.getSelling(date.get(i).getCityName());
String sortString = pinyin.substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if (sortString.matches("[A-Z]")) {
sortModel.setSortLetters(sortString.toUpperCase());
} else {
sortModel.setSortLetters("#");
}
cityList.set(i,sortModel);
}
}
/**
* 根据输入框中的值来过滤数据并更新ListView
*
* @param filterStr
*/
private void filterData(String filterStr) {
List<CityListBean> filterDateList = new ArrayList<CityListBean>();
if (TextUtils.isEmpty(filterStr)) {
filterDateList = cityList;
} else {
for (int i = 0; i < cityList.size(); i++) {
if (cityList.get(i).getCityName().contains(filterStr)) {
filterDateList.add(cityList.get(i));
}
}
}
// 根据a-z进行排序
Collections.sort(filterDateList, pinyinComparator);
adapter.updateListView(filterDateList);
}
}
SortAdapter
package com.first.mzystore.home.activity.shopmap.poi.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.first.mzystore.R;
import com.first.mzystore.home.bean.CityListBean;
import java.util.List;
public class SortAdapter extends BaseAdapter implements SectionIndexer{
private List<CityListBean> list = null;
private Context mContext;
public SortAdapter(Context mContext, List<CityListBean> list) {
this.mContext = mContext;
this.list = list;
}
/**
* 当ListView数据发生变化时,调用此方法来更新ListView
* @param list
*/
public void updateListView(List<CityListBean> list){
this.list = list;
notifyDataSetChanged();
}
public int getCount() {
return this.list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder = null;
final CityListBean mContent = list.get(position);
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item_city_selecter, null);
viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
//根据position获取分类的首字母的Char ascii值
int section = getSectionForPosition(position);
//如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
if(position == getPositionForSection(section)){
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.getSortLetters());
}else{
viewHolder.tvLetter.setVisibility(View.GONE);
}
viewHolder.tvTitle.setText(this.list.get(position).getCityName());
return view;
}
final static class ViewHolder {
TextView tvLetter;
TextView tvTitle;
}
/**
* 根据ListView的当前位置获取分类的首字母的Char ascii值
*/
public int getSectionForPosition(int position) {
return list.get(position).getSortLetters().charAt(0);
}
/**
* 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
*/
public int getPositionForSection(int section) {
for (int i = 0; i < getCount(); i++) {
String sortStr = list.get(i).getSortLetters();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
/**
* 提取英文的首字母,非英文字母用#代替。
*
* @param str
* @return
*/
private String getAlpha(String str) {
String sortStr = str.trim().substring(0, 1).toUpperCase();
// 正则表达式,判断首字母是否是英文字母
if (sortStr.matches("[A-Z]")) {
return sortStr;
} else {
return "#";
}
}
@Override
public Object[] getSections() {
return null;
}
}
Java汉字转换为拼音工具类
/*
* Filename CharacterParser.java
* Company
* @author LuRuihui
* @version 0.1
*/
package com.first.mzystore.home.activity.shopmap.poi.utils;
/**
* Java汉字转换为拼音
*
*/
public class CharacterParser
{
private static int[] pyvalue = new int[]
{ -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, -17970,
-17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363,
-15362, -15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831, -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387,
-13383, -13367, -13359, -13356, -13343, -13340, -13329, -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281, -10274, -10270, -10262,
-10260, -10256, -10254 };
public static String[] pystr = new String[]
{ "a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", "hao",
"he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken", "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu",
"nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei",
"wen", "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo" };
private StringBuilder buffer;
private String resource;
private static CharacterParser characterParser = new CharacterParser();
public static CharacterParser getInstance()
{
return characterParser;
}
public String getResource()
{
return resource;
}
public void setResource(String resource)
{
this.resource = resource;
}
/** * ����ת��ASCII�� * * @param chs * @return */
private int getChsAscii(String chs)
{
int asc = 0;
try
{
byte[] bytes = chs.getBytes("gb2312");
if (bytes == null || bytes.length > 2 || bytes.length <= 0)
{
throw new RuntimeException("illegal resource string");
}
if (bytes.length == 1)
{
asc = bytes[0];
}
if (bytes.length == 2)
{
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
asc = (256 * hightByte + lowByte) - 256 * 256;
}
} catch (Exception e)
{
System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e);
}
return asc;
}
/** * ���ֽ��� * * @param str * @return */
public String convert(String str)
{
String result = null;
int ascii = getChsAscii(str);
if (ascii > 0 && ascii < 160)
{
result = String.valueOf((char) ascii);
} else
{
for (int i = (pyvalue.length - 1); i >= 0; i--)
{
if (pyvalue[i] <= ascii)
{
result = pystr[i];
break;
}
}
}
return result;
}
public String getSelling(String chs)
{
String key, value;
buffer = new StringBuilder();
for (int i = 0; i < chs.length(); i++)
{
key = chs.substring(i, i + 1);
if (key.getBytes().length >= 2)
{
value = (String) convert(key);
if (value == null)
{
value = "unknown";
}
} else
{
value = key;
}
buffer.append(value);
}
return buffer.toString();
}
public String getSpelling()
{
return this.getSelling(this.getResource());
}
}
自定义删除按钮
package com.first.mzystore.home.activity.shopmap.poi.utils;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import com.first.mzystore.R;
public class ClearEditText extends EditText implements OnFocusChangeListener, TextWatcher
{
/**
* 删除按钮的引用
*/
private Drawable mClearDrawable;
public ClearEditText(Context context)
{
this(context, null);
}
public ClearEditText(Context context, AttributeSet attrs)
{
// 这里构造方法也很重要,不加这个很多属性不能再XML里面定义
this(context, attrs, android.R.attr.editTextStyle);
}
public ClearEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
private void init()
{
// 获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
mClearDrawable = getCompoundDrawables()[2];
if (mClearDrawable == null)
{
mClearDrawable = getResources().getDrawable(R.mipmap.regist_delete);
}
mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
setClearIconVisible(false);
setOnFocusChangeListener(this);
addTextChangedListener(this);
}
/**
* 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 当我们按下的位置 在 EditText的宽度 -
* 图标到控件右边的间距 - 图标的宽度 和 EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
*/
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (getCompoundDrawables()[2] != null)
{
if (event.getAction() == MotionEvent.ACTION_UP)
{
boolean touchable = event.getX() > (getWidth() - getPaddingRight() - mClearDrawable.getIntrinsicWidth()) && (event.getX() < ((getWidth() - getPaddingRight())));
if (touchable)
{
this.setText("");
}
}
}
return super.onTouchEvent(event);
}
/**
* 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
*/
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
setClearIconVisible(getText().length() > 0);
} else
{
setClearIconVisible(false);
}
}
/**
* 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
*
* @param visible
*/
protected void setClearIconVisible(boolean visible)
{
Drawable right = visible ? mClearDrawable : null;
setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
}
/**
* 当输入框里面内容发生变化的时候回调的方法
*/
@Override
public void onTextChanged(CharSequence s, int start, int count, int after)
{
setClearIconVisible(s.length() > 0);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
}
/**
* 设置晃动动画
*/
public void setShakeAnimation()
{
this.setAnimation(shakeAnimation(5));
}
/**
* 晃动动画
*
* @param counts
* 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts)
{
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
}
dp与px转换工具
package com.first.mzystore.home.activity.shopmap.poi.utils;
import android.content.Context;
public class DensityUtil {
/**
*
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
*
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
拼音比较工具类
package com.first.mzystore.home.activity.shopmap.poi.utils;
import com.first.mzystore.home.bean.CityListBean;
import java.util.Comparator;
public class PinyinComparator implements Comparator<CityListBean>
{
public int compare(CityListBean o1, CityListBean o2)
{
if (o1.getSortLetters().equals("@") || o2.getSortLetters().equals("#"))
{
return -1;
} else if (o1.getSortLetters().equals("#") || o2.getSortLetters().equals("@"))
{
return 1;
} else
{
return o1.getSortLetters().compareTo(o2.getSortLetters());
}
}
}
sidebar右侧字母栏
package com.first.mzystore.home.activity.shopmap.poi.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import com.first.mzystore.R;
import com.first.mzystore.home.activity.shopmap.poi.utils.DensityUtil;
public class SideBar extends View {
// 触摸事件
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
// 26个字母
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", "#" };
private int choose = -1;// 选中
private Paint paint = new Paint();
private TextView mTextDialog;
private Context mContext;
public void setTextView(TextView mTextDialog) {
this.mTextDialog = mTextDialog;
}
public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
}
public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public SideBar(Context context) {
super(context);
this.mContext = context;
}
/**
* 重写这个方法
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取焦点改变背景颜色.
int height = getHeight();// 获取对应高度
int width = getWidth(); // 获取对应宽度
int singleHeight = height / b.length;// 获取每一个字母的高度
for (int i = 0; i < b.length; i++) {
paint.setColor(Color.GRAY);
// paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(DensityUtil.dip2px(mContext, 10));
// 选中的状态
if (i == choose) {
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true);
}
// x坐标等于中间-字符串宽度的一半.
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();// 点击y坐标
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundDrawable(new ColorDrawable(0x00000000));
choose = -1;//
invalidate();
if (mTextDialog != null) {
mTextDialog.setVisibility(View.INVISIBLE);
}
break;
default:
setBackgroundResource(R.drawable.sidebar_background);
if (oldChoose != c) {
if (c >= 0 && c < b.length) {
if (listener != null) {
listener.onTouchingLetterChanged(b[c]);
}
if (mTextDialog != null) {
mTextDialog.setText(b[c]);
mTextDialog.setVisibility(View.VISIBLE);
}
choose = c;
invalidate();
}
}
break;
}
return true;
}
/**
* 向外公开的方法
*
* @param onTouchingLetterChangedListener
*/
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
* 接口
*
* @author coder
*
*/
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}
以上就是整个功能实现所涉及到的内容。
城市列表json文件下载地址 :点击打开链接
上一篇: Androidstudio学习笔记之导航栏添加Actionbar
下一篇: 百度定位sdk使用
推荐阅读