高德地图周边的推荐

原文:http://blog.youkuaiyun.com/hedong_77/article/details/54140954

先来个效果图:  这里写图片描述  蓝色的marker就是点击的,蓝色的圆圈是我当前位置。  apk下载地址:http://download.youkuaiyun.com/detail/hedong_77/9731739  一些基本操作这里就不多说了,自己去看官方文档,我们这里说一下周边搜索和POI计算距离。  首先定位一下自己当前位置,因为我要拿到当前的位置的经纬度,后面有用:

 /**
     * 设置一些amap的属性
     */
    private void setUpLocation() {
        mAMap.setLocationSource(this);// 设置定位监听
        mAMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        mAMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
        // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
        mAMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
    }

    /**
     * 定位成功后回调函数
     */
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mListener != null && amapLocation != null) {
            if (amapLocation != null
                    && amapLocation.getErrorCode() == 0) {

                //定位成功回调信息,设置相关消息
                amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                myLat = amapLocation.getLatitude();//获取纬度
                myLongt = amapLocation.getLongitude();//获取经度
                amapLocation.getAccuracy();//获取精度信息
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(amapLocation.getTime());
                df.format(date);//定位时间
                amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
                amapLocation.getCountry();//国家信息
                amapLocation.getProvince();//省信息
                amapLocation.getCity();//城市信息
                amapLocation.getDistrict();//城区信息
                amapLocation.getStreet();//街道信息
                amapLocation.getStreetNum();//街道门牌号信息
                amapLocation.getCityCode();//城市编码
                amapLocation.getAdCode();//地区编码
                TextView textView = (TextView) findViewById(R.id.city_detail);
                textView.setText(amapLocation.getCity() + "=" + amapLocation.getDistrict() + "==" + amapLocation.getStreetNum());
                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
            // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后,在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
            mlocationClient.startLocation();
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

可以看到,定位成功之后我们可以拿到任何想要的数据。 
一步步来,先初始化一下数据,

  /**
     * 初始化AMap对象
     */
    private void init() {
        if (mAMap == null) {
            mAMap = mapview.getMap();
            mAMap.setOnMapClickListener(this);
            mAMap.setOnMarkerClickListener(this);
            mAMap.setOnInfoWindowClickListener(this);
            mAMap.setInfoWindowAdapter(this);
            TextView searchButton = (TextView) findViewById(R.id.btn_search);
            searchButton.setOnClickListener(this);
            setUpLocation();
            locationMarker = mAMap.addMarker(new MarkerOptions()
                    .anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory
                            .fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.point4)))
                    .position(new LatLng(myLat, myLongt)));
            locationMarker.showInfoWindow();


        }

        setup();

        mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLat, myLongt), 14));
        tvDistance = (TextView) findViewById(R.id.poi_info);

    }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

我们可以根据自己想要的去搜索

/**
     * 开始进行poi搜索
     */
    protected void doSearchQuery() {
        keyWord = mSearchText.getText().toString().trim();
        currentPage = 0;
        query = new PoiSearch.Query(keyWord, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);// 设置查第一页

        LatLonPoint lp = new LatLonPoint(myLat, myLongt);
        if (lp != null) {
            poiSearch = new PoiSearch(this, query);
            poiSearch.setOnPoiSearchListener(this);

            poiSearch.setBound(new SearchBound(lp, 10000, true));//
            // 设置搜索区域为以lp点为圆心,其周围10000米范围
            poiSearch.searchPOIAsyn();// 异步搜索
        }
    }```
    搜索的结果会在   public void onPoiSearched(PoiResult result, int rcode) 里面来做,如果rcode==1000那就是搜索成功了
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
@Override
public void onPoiSearched(PoiResult result, int rcode) {
    if (rcode == AMapException.CODE_AMAP_SUCCESS) {
        if (result != null && result.getQuery() != null) {// 搜索poi的结果
            if (result.getQuery().equals(query)) {// 是否是同一条
                poiResult = result;
                poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
                List<SuggestionCity> suggestionCities = poiResult
                        .getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
                if (poiItems != null && poiItems.size() > 0) {
                    //清除POI信息显示
                    whetherToShowDetailInfo(false);
                    //并还原点击marker样式
                    if (mlastMarker != null) {
                        resetlastmarker();
                    }
                    //清理之前搜索结果的marker
                    if (poiOverlay != null) {
                        poiOverlay.removeFromMap();
                    }
                    mAMap.clear();
                    poiOverlay = new myPoiOverlay(mAMap, poiItems);
                    poiOverlay.addToMap();
                    poiOverlay.zoomToSpan();

                    mAMap.addMarker(new MarkerOptions()
                            .anchor(0.5f, 0.5f)
                            .icon(BitmapDescriptorFactory
                                    .fromBitmap(BitmapFactory.decodeResource(
                                            getResources(), R.mipmap.point4)))
                            .position(new LatLng(myLat, myLongt)));

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

// mAMap.addCircle(new CircleOptions() 
// .center(new LatLng(myLat, myLongt)).radius(5000) 
// .strokeColor(Color.BLUE) 
// .fillColor(Color.argb(50, 1, 1, 1)) 
// .strokeWidth(2));

                } else if (suggestionCities != null
                        && suggestionCities.size() > 0) {
                    showSuggestCity(suggestionCities);
                } else {
                    ToastUtil.show(PoiAroundSearchActivity.this,
                            R.string.no_result);
                }
            }
        } else {
            ToastUtil
                    .show(PoiAroundSearchActivity.this, R.string.no_result);
        }
    } else {
        ToastUtil.showerror(this.getApplicationContext(), rcode);
    }
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
接下来要做的就是根据搜索到的marker,当用户点击这个marker的时候,就计算出用户当前点击的marker到用户之间的距离。

  
  • 1
  • 2

@Override 
public boolean onMarkerClick(Marker marker) {

    if (marker.getObject() != null) {
        whetherToShowDetailInfo(true);
        try {
            PoiItem mCurrentPoi = (PoiItem) marker.getObject();
            if (mlastMarker == null) {
                mlastMarker = marker;
            } else {
                // 将之前被点击的marker置为原来的状态
                resetlastmarker();
                mlastMarker = marker;
            }
            detailMarker = marker;
            detailMarker.setIcon(BitmapDescriptorFactory
                    .fromBitmap(BitmapFactory.decodeResource(
                            getResources(),
                            R.mipmap.poi_marker_pressed)));

            setPoiItemDisplayContent(mCurrentPoi);
        } catch (Exception e) {
            // TODO: handle exception
        }
    } else {
        whetherToShowDetailInfo(false);
        resetlastmarker();
    }

    //我的当前位置与点击的marker的距离
    LatLng latlngA = new LatLng(myLat, myLongt);
    distance = AMapUtils.calculateLineDistance(latlngA, marker.getPosition());
    tvDistance.setText("两点间距离为:" + distance + "m");
    return true;
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

这里我只是显示十个marker。 
怎么把marker添加到地图里面呢,我们自己定义一个类吧,

 /**
     * 自定义PoiOverlay
     */
    private class myPoiOverlay {
        private AMap mamap;
        private List<PoiItem> mPois;
        private ArrayList<Marker> mPoiMarks = new ArrayList<Marker>();

        public myPoiOverlay(AMap amap, List<PoiItem> pois) {
            mamap = amap;
            mPois = pois;
        }

        /**
         * 添加Marker到地图中。
         *
         * @since V2.1.0
         */
        public void addToMap() {
            for (int i = 0; i < mPois.size(); i++) {
                Marker marker = mamap.addMarker(getMarkerOptions(i));
                PoiItem item = mPois.get(i);
                marker.setObject(item);
                mPoiMarks.add(marker);
            }
        }

        /**
         * 去掉PoiOverlay上所有的Marker。
         *
         * @since V2.1.0
         */
        public void removeFromMap() {
            for (Marker mark : mPoiMarks) {
                mark.remove();
            }
        }

        /**
         * 移动镜头到当前的视角。
         *
         * @since V2.1.0
         */
        public void zoomToSpan() {
            if (mPois != null && mPois.size() > 0) {
                if (mamap == null)
                    return;
                LatLngBounds bounds = getLatLngBounds();
                mamap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
            }
        }

        private LatLngBounds getLatLngBounds() {
            LatLngBounds.Builder b = LatLngBounds.builder();
            for (int i = 0; i < mPois.size(); i++) {
                b.include(new LatLng(mPois.get(i).getLatLonPoint().getLatitude(),
                        mPois.get(i).getLatLonPoint().getLongitude()));
            }
            return b.build();
        }

        private MarkerOptions getMarkerOptions(int index) {
            return new MarkerOptions()
                    .position(
                            new LatLng(mPois.get(index).getLatLonPoint()
                                    .getLatitude(), mPois.get(index)
                                    .getLatLonPoint().getLongitude()))
                    .title(getTitle(index)).snippet(getSnippet(index))
                    .icon(getBitmapDescriptor(index));
        }

        protected String getTitle(int index) {
            return mPois.get(index).getTitle();
        }

        protected String getSnippet(int index) {
            return mPois.get(index).getSnippet();
        }

        /**
         * 从marker中得到poi在list的位置。
         *
         * @param marker 一个标记的对象。
         * @return 返回该marker对应的poi在list的位置。
         * @since V2.1.0
         */
        public int getPoiIndex(Marker marker) {
            for (int i = 0; i < mPoiMarks.size(); i++) {
                if (mPoiMarks.get(i).equals(marker)) {
                    return i;
                }
            }
            return -1;
        }

        /**
         * 返回第index的poi的信息。
         *
         * @param index 第几个poi。
         * @return poi的信息。poi对象详见搜索服务模块的基础核心包(com.amap.api.services.core)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/core/PoiItem.html" title="com.amap.api.services.core中的类">PoiItem</a></strong>。
         * @since V2.1.0
         */
        public PoiItem getPoiItem(int index) {
            if (index < 0 || index >= mPois.size()) {
                return null;
            }
            return mPois.get(index);
        }

        protected BitmapDescriptor getBitmapDescriptor(int arg0) {
            if (arg0 < 10) {
                BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
                        BitmapFactory.decodeResource(getResources(), markers[arg0]));
                return icon;
            } else {
                BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
                        BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight));
                return icon;
            }
        }
    }
代码解释很全面,应该看懂没问题了。主要代码也就这么多。
贴一个全面的类给大家参考:

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 
import android.widget.TextView;

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.maps.AMap; 
import com.amap.api.maps.AMap.InfoWindowAdapter; 
import com.amap.api.maps.AMap.OnInfoWindowClickListener; 
import com.amap.api.maps.AMap.OnMapClickListener; 
import com.amap.api.maps.AMap.OnMarkerClickListener; 
import com.amap.api.maps.AMapUtils; 
import com.amap.api.maps.CameraUpdateFactory; 
import com.amap.api.maps.LocationSource; 
import com.amap.api.maps.MapView; 
import com.amap.api.maps.model.BitmapDescriptor; 
import com.amap.api.maps.model.BitmapDescriptorFactory; 
import com.amap.api.maps.model.LatLng; 
import com.amap.api.maps.model.LatLngBounds; 
import com.amap.api.maps.model.Marker; 
import com.amap.api.maps.model.MarkerOptions; 
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 com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener; 
import com.amap.api.services.poisearch.PoiSearch.SearchBound; 
import com.example.donghe.maptest.utils.ToastUtil;

import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List;

/** 
* 周边搜索 
*/ 
public class PoiAroundSearchActivity extends AppCompatActivity implements OnClickListener, 
OnMapClickListener, OnInfoWindowClickListener, InfoWindowAdapter, OnMarkerClickListener, 
OnPoiSearchListener, LocationSource, 
AMapLocationListener { 
private MapView mapview; 
private AMap mAMap;

private PoiResult poiResult; // poi返回的结果
private int currentPage = 0;// 当前页面,从0开始计数
private PoiSearch.Query query;// Poi查询条件类
private Marker locationMarker; // 选择的点
private Marker detailMarker;
private Marker mlastMarker;
private PoiSearch poiSearch;
private myPoiOverlay poiOverlay;// poi图层
private List<PoiItem> poiItems;// poi数据

private RelativeLayout mPoiDetail;
private TextView mPoiName, mPoiAddress;
private String keyWord = "";
private EditText mSearchText;
private TextView tvDistance;

private float distance;
private Double myLat = 0.0, myLongt = 0.0;//我的当前位置的经纬度

//定位
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.poiaroundsearch_activity);
    mapview = (MapView) findViewById(R.id.mapView);
    mapview.onCreate(savedInstanceState);
    init();
}

/**
 * 初始化AMap对象
 */
private void init() {
    if (mAMap == null) {
        mAMap = mapview.getMap();
        mAMap.setOnMapClickListener(this);
        mAMap.setOnMarkerClickListener(this);
        mAMap.setOnInfoWindowClickListener(this);
        mAMap.setInfoWindowAdapter(this);
        TextView searchButton = (TextView) findViewById(R.id.btn_search);
        searchButton.setOnClickListener(this);
        setUpLocation();
        locationMarker = mAMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .icon(BitmapDescriptorFactory
                        .fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.point4)))
                .position(new LatLng(myLat, myLongt)));
        locationMarker.showInfoWindow();


    }

    setup();

    mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLat, myLongt), 14));
    tvDistance = (TextView) findViewById(R.id.poi_info);

}

private void setup() {
    mPoiDetail = (RelativeLayout) findViewById(R.id.poi_detail);
    mPoiDetail.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

// Intent intent = new Intent(PoiSearchActivity.this, 
// SearchDetailActivity.class); 
// intent.putExtra(“poiitem”, mPoi); 
// startActivity(intent);

        }
    });
    mPoiName = (TextView) findViewById(R.id.poi_name);
    mPoiAddress = (TextView) findViewById(R.id.poi_address);
    mSearchText = (EditText) findViewById(R.id.input_edittext);
}

/**
 * 开始进行poi搜索
 */
protected void doSearchQuery() {
    keyWord = mSearchText.getText().toString().trim();
    currentPage = 0;
    query = new PoiSearch.Query(keyWord, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
    query.setPageSize(20);// 设置每页最多返回多少条poiitem
    query.setPageNum(currentPage);// 设置查第一页

    LatLonPoint lp = new LatLonPoint(myLat, myLongt);
    if (lp != null) {
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);

        poiSearch.setBound(new SearchBound(lp, 10000, true));//
        // 设置搜索区域为以lp点为圆心,其周围10000米范围
        poiSearch.searchPOIAsyn();// 异步搜索
    }
}

/**
 * 方法必须重写
 */
@Override
protected void onResume() {
    super.onResume();
    mapview.onResume();
    whetherToShowDetailInfo(false);
}

/**
 * 方法必须重写
 */
@Override
protected void onPause() {
    super.onPause();
    mapview.onPause();
}

/**
 * 方法必须重写
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapview.onSaveInstanceState(outState);
}

/**
 * 方法必须重写
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    mapview.onDestroy();
}

@Override
public void onPoiItemSearched(PoiItem arg0, int arg1) {
    // TODO Auto-generated method stub

}


@Override
public void onPoiSearched(PoiResult result, int rcode) {
    if (rcode == AMapException.CODE_AMAP_SUCCESS) {
        if (result != null && result.getQuery() != null) {// 搜索poi的结果
            if (result.getQuery().equals(query)) {// 是否是同一条
                poiResult = result;
                poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始
                List<SuggestionCity> suggestionCities = poiResult
                        .getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息
                if (poiItems != null && poiItems.size() > 0) {
                    //清除POI信息显示
                    whetherToShowDetailInfo(false);
                    //并还原点击marker样式
                    if (mlastMarker != null) {
                        resetlastmarker();
                    }
                    //清理之前搜索结果的marker
                    if (poiOverlay != null) {
                        poiOverlay.removeFromMap();
                    }
                    mAMap.clear();
                    poiOverlay = new myPoiOverlay(mAMap, poiItems);
                    poiOverlay.addToMap();
                    poiOverlay.zoomToSpan();

                    mAMap.addMarker(new MarkerOptions()
                            .anchor(0.5f, 0.5f)
                            .icon(BitmapDescriptorFactory
                                    .fromBitmap(BitmapFactory.decodeResource(
                                            getResources(), R.mipmap.point4)))
                            .position(new LatLng(myLat, myLongt)));

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

// mAMap.addCircle(new CircleOptions() 
// .center(new LatLng(myLat, myLongt)).radius(5000) 
// .strokeColor(Color.BLUE) 
// .fillColor(Color.argb(50, 1, 1, 1)) 
// .strokeWidth(2));

                } else if (suggestionCities != null
                        && suggestionCities.size() > 0) {
                    showSuggestCity(suggestionCities);
                } else {
                    ToastUtil.show(PoiAroundSearchActivity.this,
                            R.string.no_result);
                }
            }
        } else {
            ToastUtil
                    .show(PoiAroundSearchActivity.this, R.string.no_result);
        }
    } else {
        ToastUtil.showerror(this.getApplicationContext(), rcode);
    }
}


@Override
public boolean onMarkerClick(Marker marker) {

    if (marker.getObject() != null) {
        whetherToShowDetailInfo(true);
        try {
            PoiItem mCurrentPoi = (PoiItem) marker.getObject();
            if (mlastMarker == null) {
                mlastMarker = marker;
            } else {
                // 将之前被点击的marker置为原来的状态
                resetlastmarker();
                mlastMarker = marker;
            }
            detailMarker = marker;
            detailMarker.setIcon(BitmapDescriptorFactory
                    .fromBitmap(BitmapFactory.decodeResource(
                            getResources(),
                            R.mipmap.poi_marker_pressed)));

            setPoiItemDisplayContent(mCurrentPoi);
        } catch (Exception e) {
            // TODO: handle exception
        }
    } else {
        whetherToShowDetailInfo(false);
        resetlastmarker();
    }

    //我的当前位置与点击的marker的距离
    LatLng latlngA = new LatLng(myLat, myLongt);
    distance = AMapUtils.calculateLineDistance(latlngA, marker.getPosition());
    tvDistance.setText("两点间距离为:" + distance + "m");
    return true;
}

// 将之前被点击的marker置为原来的状态
private void resetlastmarker() {
    int index = poiOverlay.getPoiIndex(mlastMarker);
    if (index < 10) {
        mlastMarker.setIcon(BitmapDescriptorFactory
                .fromBitmap(BitmapFactory.decodeResource(
                        getResources(),
                        markers[index])));
    } else {
        mlastMarker.setIcon(BitmapDescriptorFactory.fromBitmap(
                BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight)));
    }
    mlastMarker = null;

}


private void setPoiItemDisplayContent(final PoiItem mCurrentPoi) {
    mPoiName.setText(mCurrentPoi.getTitle());
    mPoiAddress.setText(mCurrentPoi.getSnippet() + mCurrentPoi.getDistance());
}


@Override
public View getInfoContents(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public View getInfoWindow(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public void onInfoWindowClick(Marker arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_search:
            doSearchQuery();
            break;

        default:
            break;
    }

}

private int[] markers = {R.mipmap.poi_marker_1,
        R.mipmap.poi_marker_2,
        R.mipmap.poi_marker_3,
        R.mipmap.poi_marker_4,
        R.mipmap.poi_marker_5,
        R.mipmap.poi_marker_6,
        R.mipmap.poi_marker_7,
        R.mipmap.poi_marker_8,
        R.mipmap.poi_marker_9,
        R.mipmap.poi_marker_10
};

private void whetherToShowDetailInfo(boolean isToShow) {
    if (isToShow) {
        mPoiDetail.setVisibility(View.VISIBLE);

    } else {
        mPoiDetail.setVisibility(View.GONE);

    }
}


@Override
public void onMapClick(LatLng arg0) {
    whetherToShowDetailInfo(false);
    if (mlastMarker != null) {
        resetlastmarker();
    }
}

/**
 * poi没有搜索到数据,返回一些推荐城市的信息
 */
private void showSuggestCity(List<SuggestionCity> cities) {
    String infomation = "推荐城市\n";
    for (int i = 0; i < cities.size(); i++) {
        infomation += "城市名称:" + cities.get(i).getCityName() + "城市区号:"
                + cities.get(i).getCityCode() + "城市编码:"
                + cities.get(i).getAdCode() + "\n";
    }
    ToastUtil.show(this, infomation);

}


/**
 * 自定义PoiOverlay
 */
private class myPoiOverlay {
    private AMap mamap;
    private List<PoiItem> mPois;
    private ArrayList<Marker> mPoiMarks = new ArrayList<Marker>();

    public myPoiOverlay(AMap amap, List<PoiItem> pois) {
        mamap = amap;
        mPois = pois;
    }

    /**
     * 添加Marker到地图中。
     *
     * @since V2.1.0
     */
    public void addToMap() {
        for (int i = 0; i < mPois.size(); i++) {
            Marker marker = mamap.addMarker(getMarkerOptions(i));
            PoiItem item = mPois.get(i);
            marker.setObject(item);
            mPoiMarks.add(marker);
        }
    }

    /**
     * 去掉PoiOverlay上所有的Marker。
     *
     * @since V2.1.0
     */
    public void removeFromMap() {
        for (Marker mark : mPoiMarks) {
            mark.remove();
        }
    }

    /**
     * 移动镜头到当前的视角。
     *
     * @since V2.1.0
     */
    public void zoomToSpan() {
        if (mPois != null && mPois.size() > 0) {
            if (mamap == null)
                return;
            LatLngBounds bounds = getLatLngBounds();
            mamap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
        }
    }

    private LatLngBounds getLatLngBounds() {
        LatLngBounds.Builder b = LatLngBounds.builder();
        for (int i = 0; i < mPois.size(); i++) {
            b.include(new LatLng(mPois.get(i).getLatLonPoint().getLatitude(),
                    mPois.get(i).getLatLonPoint().getLongitude()));
        }
        return b.build();
    }

    private MarkerOptions getMarkerOptions(int index) {
        return new MarkerOptions()
                .position(
                        new LatLng(mPois.get(index).getLatLonPoint()
                                .getLatitude(), mPois.get(index)
                                .getLatLonPoint().getLongitude()))
                .title(getTitle(index)).snippet(getSnippet(index))
                .icon(getBitmapDescriptor(index));
    }

    protected String getTitle(int index) {
        return mPois.get(index).getTitle();
    }

    protected String getSnippet(int index) {
        return mPois.get(index).getSnippet();
    }

    /**
     * 从marker中得到poi在list的位置。
     *
     * @param marker 一个标记的对象。
     * @return 返回该marker对应的poi在list的位置。
     * @since V2.1.0
     */
    public int getPoiIndex(Marker marker) {
        for (int i = 0; i < mPoiMarks.size(); i++) {
            if (mPoiMarks.get(i).equals(marker)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 返回第index的poi的信息。
     *
     * @param index 第几个poi。
     * @return poi的信息。poi对象详见搜索服务模块的基础核心包(com.amap.api.services.core)中的类 <strong><a href="../../../../../../Search/com/amap/api/services/core/PoiItem.html" title="com.amap.api.services.core中的类">PoiItem</a></strong>。
     * @since V2.1.0
     */
    public PoiItem getPoiItem(int index) {
        if (index < 0 || index >= mPois.size()) {
            return null;
        }
        return mPois.get(index);
    }

    protected BitmapDescriptor getBitmapDescriptor(int arg0) {
        if (arg0 < 10) {
            BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
                    BitmapFactory.decodeResource(getResources(), markers[arg0]));
            return icon;
        } else {
            BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(
                    BitmapFactory.decodeResource(getResources(), R.mipmap.marker_other_highlight));
            return icon;
        }
    }
}

/**
 * 设置一些amap的属性
 */
private void setUpLocation() {
    mAMap.setLocationSource(this);// 设置定位监听
    mAMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
    mAMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
    // 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
    mAMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}

/**
 * 定位成功后回调函数
 */
@Override
public void onLocationChanged(AMapLocation amapLocation) {
    if (mListener != null && amapLocation != null) {
        if (amapLocation != null
                && amapLocation.getErrorCode() == 0) {

            //定位成功回调信息,设置相关消息
            amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
            myLat = amapLocation.getLatitude();//获取纬度
            myLongt = amapLocation.getLongitude();//获取经度
            amapLocation.getAccuracy();//获取精度信息
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(amapLocation.getTime());
            df.format(date);//定位时间
            amapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
            amapLocation.getCountry();//国家信息
            amapLocation.getProvince();//省信息
            amapLocation.getCity();//城市信息
            amapLocation.getDistrict();//城区信息
            amapLocation.getStreet();//街道信息
            amapLocation.getStreetNum();//街道门牌号信息
            amapLocation.getCityCode();//城市编码
            amapLocation.getAdCode();//地区编码
            TextView textView = (TextView) findViewById(R.id.city_detail);
            textView.setText(amapLocation.getCity() + "=" + amapLocation.getDistrict() + "==" + amapLocation.getStreetNum());
            mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
        } else {
            String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
            Log.e("AmapErr", errText);
        }
    }
}

/**
 * 激活定位
 */
@Override
public void activate(OnLocationChangedListener listener) {
    mListener = listener;
    if (mlocationClient == null) {
        mlocationClient = new AMapLocationClient(this);
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mlocationClient.setLocationListener(this);
        //设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
        // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后,在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
        mlocationClient.startLocation();
    }
}

/**
 * 停止定位
 */
@Override
public void deactivate() {
    mListener = null;
    if (mlocationClient != null) {
        mlocationClient.stopLocation();
        mlocationClient.onDestroy();
    }
    mlocationClient = null;
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362

}

点击打开链接



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值