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
| public class New_LocalActivity extends Activity implements LocationSource, AMapLocationListener, AMap.OnCameraChangeListener, PoiSearch.OnPoiSearchListener {
@BindView(R.id.map_local) MapView mapView; @BindView(R.id.map_list) ListView mapList;
public static final String KEY_LAT = "lat"; public static final String KEY_LNG = "lng"; public static final String KEY_DES = "des";
private AMapLocationClient mLocationClient; private LocationSource.OnLocationChangedListener mListener; private LatLng latlng; private String city; private AMap aMap; private String deepType = ""; private PoiSearch.Query query; private PoiSearch poiSearch; private PoiResult poiResult; private PoiOverlay poiOverlay; private List<PoiItem> poiItems;
private PoiSearch_adapter adapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new__local); ButterKnife.bind(this); mapView.onCreate(savedInstanceState); init(); }
private void init() { if (aMap == null) { aMap = mapView.getMap(); aMap.setOnCameraChangeListener(this); setUpMap(); }
deepType = "餐饮"; }
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(); } MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.location_marker)); myLocationStyle.strokeColor(Color.BLACK); myLocationStyle.radiusFillColor(Color.argb(100, 0, 0, 180)); myLocationStyle.strokeWidth(1.0f); aMap.setMyLocationStyle(myLocationStyle); aMap.setLocationSource(this); aMap.getUiSettings().setMyLocationButtonEnabled(true); aMap.setMyLocationEnabled(true); }
protected void doSearchQuery() { aMap.setOnMapClickListener(null); int currentPage = 0; query = new PoiSearch.Query("", deepType, city); query.setPageSize(20); 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, 5000, true)); 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, 14), 1000, null); city = aMapLocation.getProvince(); doSearchQuery(); } else { 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(); aMap.addMarker(new MarkerOptions().position(latlng)); doSearchQuery(); }
@Override public void onPoiSearched(PoiResult result, int rCode) { if (rCode == 0) { if (result != null && result.getQuery() != null) { if (result.getQuery().equals(query)) { poiResult = result; poiItems = poiResult.getPois(); List<SuggestionCity> suggestionCities = poiResult .getSearchSuggestionCitys(); if (poiItems != null && poiItems.size() > 0) { adapter = new PoiSearch_adapter(this, poiItems); mapList.setAdapter(adapter); mapList.setOnItemClickListener(new mOnItemClickListener());
} } else { Logger.d("无结果"); } } } else { Logger.e("无结果"); } } else if (rCode == 27) { Logger.e("error_network"); } else if (rCode == 32) { Logger.e("error_key"); } else { Logger.e("error_other:" + rCode); } }
@Override public void onPoiItemSearched(PoiItem poiItem, int i) {
}
@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) { Intent intent = new Intent(); intent.putExtra(KEY_LAT, poiItems.get(position).getLatLonPoint().getLatitude()); intent.putExtra(KEY_LNG, poiItems.get(position).getLatLonPoint().getLongitude()); intent.putExtra(KEY_DES, poiItems.get(position).getTitle()); setResult(RESULT_OK, intent); finish(); } }
|