001 package com.lbsproject;
002
003 import java.util.ArrayList;
004
005 import android.content.Context;
006 import android.graphics.Canvas;
007 import android.graphics.Color;
008 import android.graphics.Paint;
009 import android.graphics.Point;
010 import android.graphics.drawable.Drawable;
011 import android.location.Criteria;
012 import android.location.Location;
013 import android.location.LocationManager;
014 import android.os.Bundle;
015 import android.util.Log;
016 import android.view.Menu;
017 import android.view.MenuItem;
018 import android.view.MotionEvent;
019 import android.view.View;
020 import android.widget.Button;
021 import android.widget.EditText;
022 import android.widget.Toast;
023
024 import com.baidu.mapapi.BMapManager;
025 import com.baidu.mapapi.GeoPoint;
026 import com.baidu.mapapi.LocationListener;
027 import com.baidu.mapapi.MKAddrInfo;
028 import com.baidu.mapapi.MKDrivingRouteResult;
029 import com.baidu.mapapi.MKPoiResult;
030 import com.baidu.mapapi.MKSearch;
031 import com.baidu.mapapi.MKSearchListener;
032 import com.baidu.mapapi.MKTransitRouteResult;
033 import com.baidu.mapapi.MKWalkingRouteResult;
034 import com.baidu.mapapi.MapActivity;
035 import com.baidu.mapapi.MapController;
036 import com.baidu.mapapi.MapView;
037 import com.baidu.mapapi.MyLocationOverlay;
038 import com.baidu.mapapi.Overlay;
039 import com.baidu.mapapi.PoiOverlay;
040 import com.baidu.mapapi.Projection;
041 import com.lbsproject.LBSProjectActivity.GetOverlay;
042
043 public classLBSProjectActivity extendsMapActivity {
044 protectedstatic final String TAG = null;
045 privateBMapManager mapManager;
046 privateMapView mapView;
047 privateMapController mapController;
048 privateMKSearch mSearch = null;
049 privateGeoPoint geoPoint = new GeoPoint(122084095, 37422006);;
050 LocationListener mLocationListener =null;
051 MyLocationOverlay mLocationOverlay =null;
052 privateEditText editCity;
053 privateEditText editGeoCodeKey;
054 privateEditText areaText;
055 privateGetOverlay getOverlay;
056
057 @Override
058 publicvoid onCreate(Bundle savedInstanceState) {
059 super.onCreate(savedInstanceState);
060 setContentView(R.layout.main);
061
062 getInitLoc();// 程序打开时候获取当前位置 显示地图上
063 mapManager =new BMapManager(getApplication());
064
065 // init方法的第一个参数需填入申请的APIKey
066 mapManager.init("2150650BE0DCF874994B845CEC7B60A0518E6AFE",null);
067 super.initMapActivity(mapManager);
068 mapView = (MapView) findViewById(R.id.mapView);
069 mapView.setBuiltInZoomControls(true);
070 mapView.setDrawOverlayWhenZooming(true);
071
072 // 添加定位图层
073 mLocationOverlay =new MyLocationOverlay(this, mapView);
074 mapView.getOverlays().add(mLocationOverlay);
075 getOverlay =new GetOverlay();
076 mapView.getOverlays().add(getOverlay);
077
078 gotoLocate();
079
080 Button buttonGetPos = (Button) findViewById(R.id.buttonGetPos);//获取"我的位置的方法"
081 buttonGetPos.setOnClickListener(newView.OnClickListener() {
082
083 publicvoid onClick(View v) {
084 getInitLoc();
085 gotoLocate();
086 }
087 });
088
089 editCity = (EditText) findViewById(R.id.editTextCity);
090 editGeoCodeKey = (EditText) findViewById(R.id.editTextAera);
091 Button buttonSearchCity = (Button) findViewById(R.id.buttonSearchCity);
092 buttonSearchCity.setOnClickListener(newView.OnClickListener() {//点击按钮定位需要去的地方
093
094 @Override
095 publicvoid onClick(View v) {
096
097 mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
098 .getText().toString());
099 // mapView.getOverlays().add(getOverlay);
100
101 }
102 });
103 areaText = (EditText) findViewById(R.id.searchKeywords);
104 Button buttonSearchArea = (Button) findViewById(R.id.buttonSearchArea);
105 buttonSearchArea.setOnClickListener(newView.OnClickListener() {
106
107 @Override
108 publicvoid onClick(View v) {
109
110
111 mSearch.poiSearchNearBy(areaText.getText().toString(),// 搜索XX附近5000米范围的XXX
112 geoPoint,5000);
113
114 }
115 });
116
117 // 注册定位事件
118 mLocationListener =new LocationListener() {
119 publicvoid onLocationChanged(Location location) {
120 if(location != null) {
121 Log.i(TAG,"" + location.getLatitude());
122 Log.i(TAG,"" + location.getLongitude());
123 geoPoint =new GeoPoint(
124 (int) (location.getLatitude() * 1e6),
125 (int) (location.getLongitude() * 1e6));
126 gotoLocate();
127 }
128 }
129
130 };
131
132 mSearch =new MKSearch();//搜索服务类
133 mSearch.init(mapManager,new MKSearchListener() {
134 publicvoid onGetAddrResult(MKAddrInfo res,int error) {
135 if(error != 0) {
136 String str = String.format("错误号:%d", error);
137 Toast.makeText(LBSProjectActivity.this, str,
138 Toast.LENGTH_LONG).show();
139 return;
140 }
141
142 mapView.getController().animateTo(res.geoPt);
143
144 // String strInfo = String.format("纬度:%f 经度:%f\r\n",
145 // res.geoPt.getLatitudeE6() / 1e6,
146 // res.geoPt.getLongitudeE6() / 1e6);
147
148 geoPoint = res.geoPt;
149
150 // Toast.makeText(LBSProjectActivity.this, strInfo,
151 // Toast.LENGTH_LONG).show();
152 Drawable marker = getResources().getDrawable(
153 R.drawable.iconmarka);// 得到需要标在地图上的资源
154 marker.setBounds(0,0, marker.getIntrinsicWidth(),
155 marker.getIntrinsicHeight());// 为maker定义位置和边界
156 mapView.getOverlays().clear();
157 mapView.getOverlays().add(getOverlay);
158 mapView.getOverlays().add(
159 newOverItemT(marker, LBSProjectActivity.this,
160 res.geoPt, res.strAddr));
161 }
162
163 publicvoid onGetPoiResult(MKPoiResult res,int type, interror) {
164 if(res == null) {
165 Log.d("onGetPoiResult","the onGetPoiResult res is " + type
166 +"__" + error);
167 }else
168 Log.d("onGetPoiResult",
169 "the onGetPoiResult res is "
170 + res.getCurrentNumPois() +"__"
171 + res.getNumPages() +"__"
172 + res.getNumPois() +"__" + type + "__"
173 + error);
174
175 // 错误号可参考MKEvent中的定义
176 if(error != 0 || res == null) {
177 Log.d("onGetPoiResult","the onGetPoiResult res 0 ");
178 Toast.makeText(LBSProjectActivity.this,"抱歉,未找到结果",
179 Toast.LENGTH_LONG).show();
180 return;
181 }
182
183 ArrayList<MKPoiResult> poiResult = res.getMultiPoiResult();
184 if(poiResult != null)
185 Log.d("onGetPoiResult","the onGetPoiResult res 1__"
186 + poiResult.size());
187 // 将地图移动到第一个POI中心点
188 if(res.getCurrentNumPois() > 0) {
189 Log.d("onGetPoiResult","the onGetPoiResult res 2");
190 // 将poi结果显示到地图上
191 PoiOverlay poiOverlay =new PoiOverlay(
192 LBSProjectActivity.this, mapView);
193 poiOverlay.setData(res.getAllPoi());
194 mapView.getOverlays().clear();
195 mapView.getOverlays().add(getOverlay);
196 mapView.getOverlays().add(poiOverlay);
197 mapView.invalidate();
198 mapView.getController().animateTo(res.getPoi(0).pt);
199 }else if (res.getCityListNum() > 0) {
200 Log.d("onGetPoiResult","the onGetPoiResult res 3");
201 String strInfo ="在";
202 for(int i = 0; i < res.getCityListNum(); i++) {
203 strInfo += res.getCityListInfo(i).city;
204 strInfo +=",";
205 }
206 strInfo +="找到结果";
207 Toast.makeText(LBSProjectActivity.this, strInfo,
208 Toast.LENGTH_LONG).show();
209 }
210
211 Log.d("onGetPoiResult","the onGetPoiResult res 4");
212
213 }
214
215 publicvoid onGetDrivingRouteResult(MKDrivingRouteResult res,
216 interror) {
217 }
218
219 publicvoid onGetTransitRouteResult(MKTransitRouteResult res,
220 interror) {
221 }
222
223 publicvoid onGetWalkingRouteResult(MKWalkingRouteResult res,
224 interror) {
225 }
226
227 });
228
229 }
230
231 privatevoid gotoLocate() {// 获取所在位置
232 Drawable marker = getResources().getDrawable(R.drawable.iconmarka);// 得到需要标在地图上的资源
233 marker.setBounds(0,0, marker.getIntrinsicWidth(),
234 marker.getIntrinsicHeight());// 为maker定义位置和边界
235 mapView.getOverlays().clear();
236 mapView.getOverlays().add(getOverlay);
237 mapView.getOverlays().add(
238 newOverItemT(marker, LBSProjectActivity.this, geoPoint,""));
239
240 mapView.getController().animateTo(geoPoint);
241 mapController = mapView.getController();
242 // 设置地图的中心
243 mapController.setCenter(geoPoint);
244 // 设置地图默认的缩放级别
245 mapController.setZoom(16);
246 }
247
248 privatevoid getInitLoc() {// 初始化时候获取坐标
249 try{
250
251 LocationManager locationManager;
252 String context = Context.LOCATION_SERVICE;
253 locationManager = (LocationManager) getSystemService(context);
254 // String provider = LocationManager.GPS_PROVIDER;
255
256 Criteria criteria =new Criteria();
257 criteria.setAccuracy(Criteria.ACCURACY_FINE);
258 criteria.setAltitudeRequired(false);
259 criteria.setBearingRequired(false);
260 criteria.setCostAllowed(true);
261 criteria.setPowerRequirement(Criteria.POWER_LOW);
262 String provider = locationManager.getBestProvider(criteria,true);
263 Location location = locationManager.getLastKnownLocation(provider);
264 geoPoint =new GeoPoint((int) (location.getLatitude() * 1e6),
265 (int) (location.getLongitude() * 1e6));
266 } catch (Exception e) {
267 // TODO: handle exception
268 }
269 }
270
271 @Override
272 protectedboolean isRouteDisplayed() {
273 returnfalse;
274 }
275
276 @Override
277 protectedvoid onDestroy() {
278 if(mapManager != null) {
279 // 程序退出前需调用此方法
280 mapManager.destroy();
281 mapManager =null;
282 }
283 super.onDestroy();
284 }
285
286 @Override
287 protectedvoid onPause() {
288 if(mapManager != null) {
289 // 终止百度地图API
290 mapManager.getLocationManager().removeUpdates(mLocationListener);
291 mLocationOverlay.disableMyLocation();
292 mLocationOverlay.disableCompass();// 关闭指南针
293 mapManager.stop();
294 }
295 super.onPause();
296 }
297
298 @Override
299 protectedvoid onResume() {
300 if(mapManager != null) {
301 // 开启百度地图API
302 // 注册定位事件,定位后将地图移动到定位点
303 mapManager.getLocationManager().requestLocationUpdates(
304 mLocationListener);
305 mLocationOverlay.enableMyLocation();
306 mLocationOverlay.enableCompass();// 打开指南针
307 mapManager.start();
308 }
309 super.onResume();
310 }
311
312 /**
313 * * 实现MKSearchListener接口,用于实现异步搜索服务 * @author liufeng
314 */
315 publicclass MySearchListener implementsMKSearchListener {
316
317 publicvoid onGetAddrResult(MKAddrInfo result,int iError) {
318 }
319
320 publicvoid onGetDrivingRouteResult(MKDrivingRouteResult result,
321 intiError) {
322 }
323
324 /**
325 * * POI搜索结果(范围检索、城市POI检索、周边检索) * * @param result 搜索结果 * @param type
326 * 返回结果类型(11,12,21:poi列表 7:城市列表) * @param iError 错误号(0表示正确返回)
327 */
328 @Override
329 publicvoid onGetPoiResult(MKPoiResult result,int type, intiError) {
330 if(result == null) {
331 return;
332 }
333 // PoiOverlay是baidu map api提供的用于显示POI的Overlay
334 PoiOverlay poioverlay =new PoiOverlay(LBSProjectActivity.this,
335 mapView);
336 // 设置搜索到的POI数据
337 poioverlay.setData(result.getAllPoi());
338 // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)
339 mapView.getOverlays().add(poioverlay);
340 }
341
342
343 publicvoid onGetTransitRouteResult(MKTransitRouteResult result,
344 intiError) {
345 }
346
347
348 publicvoid onGetWalkingRouteResult(MKWalkingRouteResult result,
349 intiError) {
350 }
351 }
352
353 classGetOverlay extends Overlay {
354 GeoPoint geo;
355
356 @Override
357 publicvoid draw(Canvas canvas, MapView gmapView,boolean arg2) {
358 super.draw(canvas, mapView, arg2);
359 if(geo == null) {
360 return;
361 }
362 Log.i("11111111111111111111", arg2 +"-------draw--");
363 }
364
365 @Override
366 publicboolean onTap(GeoPoint geo, MapView arg1) {
367 geoPoint = geo;
368 Drawable marker = getResources().getDrawable(R.drawable.iconmarka);// 得到需要标在地图上的资源
369 marker.setBounds(0,0, marker.getIntrinsicWidth(),
370 marker.getIntrinsicHeight());// 为maker定义位置和边界
371 mapView.getOverlays().clear();
372 mapView.getOverlays().add(getOverlay);
373 mapView.getOverlays()
374 .add(newOverItemT(marker, LBSProjectActivity.this,
375 geoPoint,""));
376 Log.i("11111111111111111111", geo.getLongitudeE6() / 1E6
377 +"----------" + geo.getLatitudeE6() / 1E6);
378 returnsuper.onTap(geo, arg1);
379 }
380
381 }
382
383 privatestatic final int TOOLBAR0 = 0;
384 privatestatic final int TOOLBAR1 = 1;
385 privatestatic final int TOOLBAR2 = 2;
386 privatestatic final int TOOLBAR3 = 3;
387
388 publicboolean onCreateOptionsMenu(Menu menu) {
389 menu.add(0, TOOLBAR0,1, "KTV").setIcon(
390 android.R.drawable.ic_btn_speak_now);
391 menu.add(0, TOOLBAR1,2, "学校").setIcon(
392 android.R.drawable.ic_menu_myplaces);
393 menu.add(0, TOOLBAR2,3, "餐厅").setIcon(
394 android.R.drawable.ic_menu_my_calendar);
395 menu.add(0, TOOLBAR3,4, "公园").setIcon(
396 android.R.drawable.ic_menu_gallery);
397 returnsuper.onCreateOptionsMenu(menu);
398 }
399
400 @Override
401 publicboolean onOptionsItemSelected(MenuItem item) {
402 switch(item.getItemId()) {
403 case0:
404 mSearch.poiSearchNearBy("KTV", geoPoint,5000);//搜索ktv
405 break;
406 case1:
407 mSearch.poiSearchNearBy("学校", geoPoint,5000);//.搜索学校
408 break;
409 case2:
410 mSearch.poiSearchNearBy("餐厅", geoPoint,5000);//搜索餐厅
411 break;
412 case3:
413 mSearch.poiSearchNearBy("公园", geoPoint,5000);//搜索公园
414 break;
415 }
416
417 returnsuper.onOptionsItemSelected(item);
418 }
419
420 }

更多相关文章

  1. android 获取通讯录中同一联系人的多个号码
  2. Android(安卓)各种音量的获取和设置
  3. Android乐动力V5.75最新获Key方法,提交步数,QQ登陆获取key案例
  4. Android中获取指定日期是星期几
  5. Android之录音工具类
  6. android 获取屏幕的长和宽
  7. android 常用代码
  8. android Rect的使用
  9. Android开发遇到的问题----自定义RecyclerView Adapter Item 显

随机推荐

  1. sqlserver复制数据库的方法步骤(图文)
  2. SQL Server 数据库分区分表(水平分表)详细
  3. SQL Server索引超出了数组界限的解决方案
  4. SQL update select结合语句详解及应用
  5. SQL Server模糊查询的常见方法总结
  6. 由于系统错误 126 (SQL Server),指定驱动
  7. 关于喜忧参半的SQL Server触发器详解
  8. 浅析SQL Server的嵌套存储过程中使用同名
  9. SQL server高并发生成唯一订单号的方法实
  10. 解密新型SQL Server无文件持久化恶意程序