Android之仿美团和百度外卖选址控件

来源:互联网 发布:oracle sql% 编辑:程序博客网 时间:2024/05/29 10:08


-----------------转载请注明出处:http://blog.csdn.net/android_cll

一:先来张效果图:




二:实现步骤:

1.选址控件xml布局实现、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffff"    android:orientation="vertical">    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="2">        <com.amap.api.maps2d.MapView            android:id="@+id/map_local"            android:layout_width="match_parent"            android:layout_height="match_parent"></com.amap.api.maps2d.MapView>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerHorizontal="true"            android:layout_centerVertical="true"            android:layout_gravity="center"            android:src="@mipmap/gps_position"            android:translationY="-18dp" />    </RelativeLayout>    <ListView        android:id="@+id/map_list"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="3"        android:background="#ffffff"        android:divider="#ffffff"        android:dividerHeight="0.5dp"        android:scrollbars="none"></ListView></LinearLayout>


2.选址控件Activity实现(代码都有注释就不多说了)、

package bzmapselection.com.mapselection;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps2d.AMap;import com.amap.api.maps2d.CameraUpdateFactory;import com.amap.api.maps2d.LocationSource;import com.amap.api.maps2d.MapView;import com.amap.api.maps2d.model.CameraPosition;import com.amap.api.maps2d.model.LatLng;import com.amap.api.maps2d.overlay.PoiOverlay;import com.amap.api.services.core.AMapException;import com.amap.api.services.core.LatLonPoint;import com.amap.api.services.core.PoiItem;import com.amap.api.services.core.SuggestionCity;import com.amap.api.services.poisearch.PoiResult;import com.amap.api.services.poisearch.PoiSearch;import java.util.List;/** * Created by Administrator on 2017/7/10. */public class AddressMapActivity extends Activity implements LocationSource, AMapLocationListener, AMap.OnCameraChangeListener, PoiSearch.OnPoiSearchListener {    private MapView mapView;    private ListView mapList;    private AMapLocationClient mLocationClient;    private OnLocationChangedListener mListener;    private LatLng latlng;    private String city;    private AMap aMap;    private String deepType = "";// poi搜索类型    private PoiSearch.Query query;// Poi查询条件类    private PoiSearch poiSearch;    private PoiResult poiResult; // poi返回的结果    private PoiOverlay poiOverlay;// poi图层    private List<PoiItem> poiItems;// poi数据    private String latj;    private String longaj;    private PoiListAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.addressmapactivity);        mapView = (MapView) findViewById(R.id.map_local);        mapList = (ListView) findViewById(R.id.map_list);        mapView.onCreate(savedInstanceState);        init();    }    private void init() {        if (aMap == null) {            aMap = mapView.getMap();            aMap.setOnCameraChangeListener(this);            setUpMap();        }        deepType = "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息";//这里以餐饮为例    }    //-------- 定位 Start ------    private void setUpMap() {        if (mLocationClient == null) {            mLocationClient = new AMapLocationClient(getApplicationContext());            AMapLocationClientOption mLocationOption = new AMapLocationClientOption();            //设置定位监听            mLocationClient.setLocationListener(this);            //设置为高精度定位模式            mLocationOption.setOnceLocation(true);            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);            //设置定位参数            mLocationClient.setLocationOption(mLocationOption);            mLocationClient.startLocation();        }        aMap.setLocationSource(this);// 设置定位监听        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false    }    /**     * 开始进行poi搜索     */    protected void doSearchQuery() {        latj = String.valueOf(latlng.latitude);        longaj = String.valueOf(latlng.longitude);        aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件        int currentPage = 0;        query = new PoiSearch.Query("", deepType, "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个city参数表示poi搜索区域(空字符串代表全国)        query.setPageSize(20);// 设置每页最多返回多少条poiitem        query.setPageNum(currentPage);// 设置查第一页        LatLonPoint lp = new LatLonPoint(latlng.latitude, latlng.longitude);        poiSearch = new PoiSearch(this, query);        poiSearch.setOnPoiSearchListener(this);        poiSearch.setBound(new PoiSearch.SearchBound(lp, 2000, true));        // 设置搜索区域为以lp点为圆心,其周围2000米范围        poiSearch.searchPOIAsyn();// 异步搜索    }    @Override    public void onLocationChanged(AMapLocation aMapLocation) {        if (mListener != null && aMapLocation != null) {            if (aMapLocation.getErrorCode() == 0) {                // 显示我的位置                mListener.onLocationChanged(aMapLocation);                //设置第一次焦点中心                latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 17), 1000, null);                city = aMapLocation.getProvince();                doSearchQuery();            } else {                Toast.makeText(AddressMapActivity.this, "定位失败,请打开定位权限", Toast.LENGTH_SHORT).show();                String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();                Log.e("AmapErr", errText);            }        }    }    @Override    public void activate(OnLocationChangedListener listener) {        mListener = listener;        mLocationClient.startLocation();    }    @Override    public void deactivate() {        mListener = null;        if (mLocationClient != null) {            mLocationClient.stopLocation();            mLocationClient.onDestroy();        }        mLocationClient = null;    }    @Override    public void onCameraChange(CameraPosition cameraPosition) {    }    @Override    public void onCameraChangeFinish(CameraPosition cameraPosition) {        latlng = cameraPosition.target;        aMap.clear();        doSearchQuery();    }    @Override    public void onPoiSearched(PoiResult result, int rCode) {        if (rCode == AMapException.CODE_AMAP_SUCCESS) {            // 搜索poi的结果            if (result != null && result.getQuery() != null) {                // 是否是同一条                if (result.getQuery().equals(query)) {                    poiResult = result;                    poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始                    List<SuggestionCity> suggestionCities = poiResult                            .getSearchSuggestionCitys();                    if (poiItems != null && poiItems.size() > 0) {                        adapter = new PoiListAdapter(this, poiItems);                        mapList.setAdapter(adapter);                        mapList.setOnItemClickListener(new mOnItemClickListener());                    } else {                        System.out.println("无结果");                    }                }            } else {                System.out.println("无结果");            }        } else if (rCode == 27) {            System.out.println("error_network");        } else if (rCode == 32) {            System.out.println("error_key");        } else {            System.out.println("error_other:" + rCode);        }    }    @Override    public void onPoiItemSearched(PoiItem poiItem, int i) {    }    //-------- 定位 End ------    @Override    protected void onResume() {        super.onResume();        mLocationClient.startLocation();    }    @Override    protected void onPause() {        super.onPause();        mLocationClient.stopLocation();    }    @Override    protected void onDestroy() {        mLocationClient.onDestroy();        super.onDestroy();    }    private class mOnItemClickListener implements AdapterView.OnItemClickListener {        @Override        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {            String lat = String.valueOf(poiItems.get(position).getLatLonPoint().getLatitude());            String longa = String.valueOf(poiItems.get(position).getLatLonPoint().getLongitude());            Intent intent = new Intent();            intent.putExtra("lat", latj);            intent.putExtra("long", longaj);            intent.putExtra("title", poiItems.get(position).getTitle());            intent.putExtra("adname", poiItems.get(position).getAdName());            setResult(RESULT_OK, intent);            finish();        }    }}


3.导入第三方jar包和so文件并引用、



4.去高德官方申请应用KEY在AndroidManifest.xml的Application下申明、



5.在AndroidManifest.xml给相应的权限、

<!--高德权限--><!--地图包、搜索包需要的基础权限--><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!--定位包、导航包需要的额外权限(注:基础权限也需要)--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /><uses-permission android:name="android.permission.WRITE_SETTINGS" />

------------到这功能就实现差不多了,欢迎大家转载,转载请注明出处、

------------最后给附上源码:http://download.csdn.net/download/android_cll/9894084


阅读全文
1 0