- 浏览: 1030387 次
文章分类
最新评论
-
yj963552657:
楼主,请问一下你用的是什么IDE?
浅谈Windows Socket程序设计(TCP和UDP) -
haohao-xuexi02:
我最喜欢那一段,家庭,孩子。。那一切都是那么顺利,值得。。
典型80后的5年工作总结 -
yunzhu:
转载的怎么可以把原文中声明原创的内容删除了,这有点不太厚道啊 ...
打破常规——大胆尝试在路由器上搭建SVN服务器 -
zhuchao_ko:
与君共勉。
典型80后的5年工作总结 -
香煎马鲛鱼:
我还在上大学,看了你的文章突然觉得生活好残忍……
典型80后的5年工作总结
手机自动定位 百度地图API使用的文章
最近由于项目需要,研究了下百度地图定位,他们提供的实例基本都是用监听器实现自动定位的。我想实现一种效果:当用户进入UI时,不定位,用户需要定位的时候,自己手动点击按钮,再去定位当前位置。 经过2天研究和咨询,找到了解决方案,在此备忘一下。
注意:定位使用真机才能够真正定位;模拟器的话,在DDMS中的Emulator Control中,选择Manual,下面单选按钮选择Decimal,然后填写经纬度,send后,再点击定位我的位置按钮,就能定位了(这应该算是固定定位,哈哈。。。)、
1、第一步当然是获取一个针对自己项目的key值。http://dev.baidu.com/wiki/static/imap/key/
2、使用百度API是有前提的,摘自百度:首先将API包括的两个文件baidumapapi.jar和libBMapApiEngine.so拷贝到工程根目录及libs\armeabi目录下,并在工程属性->Java Build Path->Libraries中选择“Add JARs”,选定baidumapapi.jar,确定后返回,这样您就可以在您的程序中使用API了。(这两个文件见附件)。
3、按照自己的需求写一个layout,我的如下:
<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/myLocation_id"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="15dp"
- android:gravity="center_horizontal"
- android:textColor="@drawable/black"
- android:background="@drawable/gary"
- />
- <com.baidu.mapapi.MapViewandroid:id="@+id/bmapsView"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- android:clickable="true"android:layout_weight="1"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/location_button_id"
- android:text="@string/location_button_text"
- />
- </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myLocation_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15dp" android:gravity="center_horizontal" android:textColor="@drawable/black" android:background="@drawable/gary" /> <com.baidu.mapapi.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/location_button_id" android:text="@string/location_button_text" /> </LinearLayout>
需要特别注意的是:<com.baidu.mapapi.MapView /> 这玩意。
4、写一个MapApplication实现application,提供全局的BMapManager,以及其初始化。
- publicBMapManagermapManager=null;
- staticMapApplicationapp;
- publicStringmStrKey="你申请的key值";
- @Override
- publicvoidonCreate(){
- mapManager=newBMapManager(this);
- mapManager.init(mStrKey,newMyGeneralListener());
- }
- @Override
- //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
- publicvoidonTerminate(){
- //TODOAuto-generatedmethodstub
- if(mapManager!=null)
- {
- mapManager.destroy();
- mapManager=null;
- }
- super.onTerminate();
- }
- staticclassMyGeneralListenerimplementsMKGeneralListener{
- @Override
- publicvoidonGetNetworkState(intarg0){
- Toast.makeText(MapApplication.app.getApplicationContext(),"您的网络出错啦!",
- Toast.LENGTH_LONG).show();
- }
- @Override
- publicvoidonGetPermissionState(intiError){
- if(iError==MKEvent.ERROR_PERMISSION_DENIED){
- //授权Key错误:
- Toast.makeText(MapApplication.app.getApplicationContext(),"您的授权Key不正确!",
- Toast.LENGTH_LONG).show();
- }
- }
- }
- 5、接下来就是按照百度api写定位代码了,使用handler机制去添加定位图层,需要说明的都在注释上了。
- privateBMapManagermBMapMan=null;
- privateMapViewmMapView=null;
- privateMapControllerbMapController;
- privateMKLocationManagermkLocationManager;
- privateMKSearchmkSearch;
- privateTextViewaddress_view;//定位到的位置信息
- privateProgressDialogdialog;
- privateList<HotelInfo>hotelList;
- privateintdistance=1000;//查询的范围(单位:m)
- Handlerhandler=newHandler(){
- @Override
- publicvoidhandleMessage(Messagemsg){
- doublelat=msg.getData().getDouble("lat");
- doublelon=msg.getData().getDouble("lon");
- if(lat!=0&&lon!=0){
- GeoPointpoint=newGeoPoint(
- (int)(lat*1E6),
- (int)(lon*1E6));
- bMapController.animateTo(point);//设置地图中心点
- bMapController.setZoom(15);
- mkSearch.reverseGeocode(point);//解析地址(异步方法)
- MyLocationOverlaymyLoc=newMyLocationOverlayFromMap(ShowMapAct.this,mMapView);
- myLoc.enableMyLocation();//启用定位
- myLoc.enableCompass();//启用指南针
- mMapView.getOverlays().add(myLoc);
- }else{
- Toast.makeText(ShowMapAct.this,"没有加载到您的位置",Toast.LENGTH_LONG).show();
- }
- if(hotelList!=null){
- Drawablemarker=getResources().getDrawable(R.drawable.iconmarka);//设置marker
- marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());//为maker定义位置和边界
- mMapView.getOverlays().add(newOverItemList(marker,hotelList,ShowMapAct.this,bMapController));
- }elseif(hotelList==null&&lat!=0&&lon!=0){
- Toast.makeText(ShowMapAct.this,"网络异常,没有获取到酒店信息。",Toast.LENGTH_LONG).show();
- }
- if(dialog!=null)dialog.dismiss();
- }
- };
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- distance=getIntent().getExtras().getInt("distance");//获取查询范围
- super.onCreate(savedInstanceState);
- setContentView(R.layout.location);
- mMapView=(MapView)findViewById(R.id.bmapsView);//初始化一个mapView存放Map
- init();//初始化地图管理器
- super.initMapActivity(mBMapMan);
- address_view=(TextView)findViewById(R.id.myLocation_id);
- SpannableStringBuilderstyle=newSpannableStringBuilder(String.format(getResources().getString(R.string.location_text),"位置不详"));
- style.setSpan(newForegroundColorSpan(Color.RED),5,style.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- address_view.setText(style);
- Buttonlocation_button=(Button)findViewById(R.id.location_button_id);
- location_button.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- dialog=ProgressDialog.show(ShowMapAct.this,"","数据加载中,请稍后.....");
- newThread(newMyThread()).start();
- }
- });
- mkSearch=newMKSearch();//初始化一个MKSearch,根据location解析详细地址
- mkSearch.init(mBMapMan,this);
- mMapView.setBuiltInZoomControls(true);//启用内置的缩放控件
- bMapController=mMapView.getController();
- GeoPointdefaultPoint=newGeoPoint((int)(39.920934*1E6),(int)(116.412817*1E6));//用给定的经纬度构造一个GeoPoint,单位是微度(度*1E6)
- bMapController.setCenter(defaultPoint);//设置地图中心点
- bMapController.setZoom(12);//设置地图zoom级别
- mkLocationManager=mBMapMan.getLocationManager();
- }
- /**
- *初始化地图管理器BMapManager
- */
- publicvoidinit(){
- MapApplicationapp=(MapApplication)getApplication();
- if(app.mapManager==null){
- app.mapManager=newBMapManager(getApplication());
- app.mapManager.init(app.mStrKey,newMapApplication.MyGeneralListener());
- }
- mBMapMan=app.mapManager;
- }
- @Override
- protectedvoidonDestroy(){
- MapApplicationapp=(MapApplication)getApplication();
- if(mBMapMan!=null){
- mBMapMan.destroy();
- app.mapManager.destroy();
- app.mapManager=null;
- mBMapMan=null;
- }
- super.onDestroy();
- }
- @Override
- protectedvoidonPause(){
- if(mBMapMan!=null){
- //终止百度地图API
- mBMapMan.stop();
- }
- super.onPause();
- }
- @Override
- protectedvoidonResume(){
- if(mBMapMan!=null){
- //开启百度地图API
- mBMapMan.start();
- }
- super.onResume();
- }
- @Override
- protectedbooleanisRouteDisplayed(){
- returnfalse;
- }
- @Override
- publicvoidonGetAddrResult(MKAddrInforesult,intiError){
- if(result==null)return;
- SpannableStringBuilderstyle=newSpannableStringBuilder(String.format(getResources().getString(R.string.location_text),result.strAddr));
- style.setSpan(newForegroundColorSpan(Color.RED),5,style.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- address_view.setText(style);
- if(dialog!=null)dialog.dismiss();
- }
- @Override
- publicvoidonGetDrivingRouteResult(MKDrivingRouteResultarg0,intarg1){}
- @Override
- publicvoidonGetPoiResult(MKPoiResultarg0,intarg1,intarg2){}
- @Override
- publicvoidonGetTransitRouteResult(MKTransitRouteResultarg0,intarg1){}
- @Override
- publicvoidonGetWalkingRouteResult(MKWalkingRouteResultarg0,intarg1){}
- /**
- *重新定位,加载数据
- *@authorAdministrator
- *
- */
- classMyThreadimplementsRunnable{
- @Override
- publicvoidrun(){
- /**
- *最重要的就是这个玩意
- *由于LocationListener获取第一个位置修正的时间会很长,为了避免用户等待,
- *在LocationListener获取第一个更精确的位置之前,应当使用getLocationInfo()获取一个缓存的位置
- */
- Locationlocation=mkLocationManager.getLocationInfo();
- doublelat=0d,lon=0d;
- if(location!=null){//定位到位置
- Stringcoordinate=location.getLatitude()+","+location.getLongitude();
- HotelRemoteDatahotelData=newHotelRemoteData();
- /**
- *远程获取酒店列表数据
- */
- hotelList=hotelData.getHotelToMap(coordinate,distance);
- lat=location.getLatitude();
- lon=location.getLongitude();
- }
- Messagemsg=newMessage();
- Bundledata=newBundle();
- data.putDouble("lat",lat);
- data.putDouble("lon",lon);
- msg.setData(data);
- handler.sendMessage(msg);
- }
- }
public BMapManager mapManager = null; static MapApplication app; public String mStrKey = "你申请的key值"; @Override public void onCreate() { mapManager = new BMapManager(this); mapManager.init(mStrKey, new MyGeneralListener()); } @Override //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗 public void onTerminate() { // TODO Auto-generated method stub if(mapManager != null) { mapManager.destroy(); mapManager = null; } super.onTerminate(); } static class MyGeneralListener implements MKGeneralListener{ @Override public void onGetNetworkState(int arg0) { Toast.makeText(MapApplication.app.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } @Override public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(MapApplication.app.getApplicationContext(),"您的授权Key不正确!", Toast.LENGTH_LONG).show(); } } } 5、接下来就是按照百度api写定位代码了,使用handler机制去添加定位图层,需要说明的都在注释上了。 private BMapManager mBMapMan = null; private MapView mMapView = null; private MapController bMapController; private MKLocationManager mkLocationManager; private MKSearch mkSearch; private TextView address_view; //定位到的位置信息 private ProgressDialog dialog; private List<HotelInfo> hotelList; private int distance = 1000; //查询的范围(单位:m) Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { double lat = msg.getData().getDouble("lat"); double lon = msg.getData().getDouble("lon"); if(lat!=0&&lon!=0){ GeoPoint point = new GeoPoint( (int) (lat * 1E6), (int) (lon * 1E6)); bMapController.animateTo(point); //设置地图中心点 bMapController.setZoom(15); mkSearch.reverseGeocode(point); //解析地址(异步方法) MyLocationOverlay myLoc = new MyLocationOverlayFromMap(ShowMapAct.this,mMapView); myLoc.enableMyLocation(); // 启用定位 myLoc.enableCompass(); // 启用指南针 mMapView.getOverlays().add(myLoc); }else{ Toast.makeText(ShowMapAct.this, "没有加载到您的位置", Toast.LENGTH_LONG).show(); } if(hotelList!=null){ Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //设置marker marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); //为maker定义位置和边界 mMapView.getOverlays().add(new OverItemList(marker,hotelList,ShowMapAct.this,bMapController)); }else if(hotelList==null&&lat!=0&&lon!=0){ Toast.makeText(ShowMapAct.this, "网络异常,没有获取到酒店信息。", Toast.LENGTH_LONG).show(); } if(dialog!=null) dialog.dismiss(); } }; @Override protected void onCreate(Bundle savedInstanceState) { distance = getIntent().getExtras().getInt("distance"); //获取查询范围 super.onCreate(savedInstanceState); setContentView(R.layout.location); mMapView = (MapView)findViewById(R.id.bmapsView); //初始化一个mapView 存放Map init(); //初始化地图管理器 super.initMapActivity(mBMapMan); address_view = (TextView)findViewById(R.id.myLocation_id); SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),"位置不详")); style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); address_view.setText(style); Button location_button = (Button)findViewById(R.id.location_button_id); location_button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { dialog = ProgressDialog.show(ShowMapAct.this, "", "数据加载中,请稍后....."); new Thread(new MyThread()).start(); } }); mkSearch = new MKSearch(); //初始化一个MKSearch,根据location解析详细地址 mkSearch.init(mBMapMan, this); mMapView.setBuiltInZoomControls(true); //启用内置的缩放控件 bMapController = mMapView.getController(); GeoPoint defaultPoint = new GeoPoint((int) (39.920934 * 1E6),(int) (116.412817 * 1E6)); //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6) bMapController.setCenter(defaultPoint); //设置地图中心点 bMapController.setZoom(12); //设置地图zoom级别 mkLocationManager = mBMapMan.getLocationManager(); } /** * 初始化地图管理器BMapManager */ public void init(){ MapApplication app = (MapApplication)getApplication(); if (app.mapManager == null) { app.mapManager = new BMapManager(getApplication()); app.mapManager.init(app.mStrKey, new MapApplication.MyGeneralListener()); } mBMapMan = app.mapManager; } @Override protected void onDestroy() { MapApplication app = (MapApplication)getApplication(); if (mBMapMan != null) { mBMapMan.destroy(); app.mapManager.destroy(); app.mapManager = null; mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { if (mBMapMan != null) { // 终止百度地图API mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { if (mBMapMan != null) { // 开启百度地图API mBMapMan.start(); } super.onResume(); } @Override protected boolean isRouteDisplayed() { return false; } @Override public void onGetAddrResult(MKAddrInfo result, int iError) { if(result==null) return; SpannableStringBuilder style = new SpannableStringBuilder(String.format(getResources().getString(R.string.location_text),result.strAddr)); style.setSpan(new ForegroundColorSpan(Color.RED), 5, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); address_view.setText(style); if(dialog!=null) dialog.dismiss(); } @Override public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {} @Override public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {} @Override public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {} @Override public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {} /** * 重新定位,加载数据 * @author Administrator * */ class MyThread implements Runnable{ @Override public void run() { /** * 最重要的就是这个玩意 * 由于LocationListener获取第一个位置修正的时间会很长,为了避免用户等待, * 在LocationListener获取第一个更精确的位置之前,应当使用getLocationInfo() 获取一个缓存的位置 */ Location location = mkLocationManager.getLocationInfo(); double lat = 0d,lon = 0d; if(location!=null){ //定位到位置 String coordinate = location.getLatitude()+","+location.getLongitude(); HotelRemoteData hotelData = new HotelRemoteData(); /** * 远程获取酒店列表数据 */ hotelList = hotelData.getHotelToMap(coordinate,distance); lat = location.getLatitude(); lon = location.getLongitude(); } Message msg = new Message(); Bundle data = new Bundle(); data.putDouble("lat", lat); data.putDouble("lon", lon); msg.setData(data); handler.sendMessage(msg); } }
6、还有一种就是百度示例相当推荐的,也是加载定位位置速度比较快的,那就是通过定位监听器来定位信息。没啥难的,照着百度的示例写,都能搞定。
- LocationListenerlistener=newLocationListener(){
- @Override
- /**位置变化,百度地图即会调用该方法去获取位置信息。
- *(我测试发现就算手机不动,它也会偶尔重新去加载位置;只要你通过重力感应,他就一定会重新加载)
- */
- publicvoidonLocationChanged(Locationlocation){
- GeoPointgp=newGeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));//通过地图上的经纬度转换为地图上的坐标点
- bMapController.animateTo(gp);//动画般的移动到定位的位置
- }
- };
相关推荐
在前端调用百度地图API,实现直接定位功能.简单易懂,物超所值
百度地图API是为开发者免费提供的一套基于百度地图服务的应用接口,包括JavaScript API、Web服务API、Android SDK、iOS SDK、定位SDK、车联网API、LBS云等多种开发工具与服务,提供基本地图展现、搜索、定位、逆/...
将百度地图嵌入c# cs窗体中,... 查了很多资料,参考网上众多朋友的代码及百度地图API demo,这个例子中的js代码不用key,方便很多,使用的是v1.4版,少了很多限制,使用.net4.5开发的,可以将c#代码转为低版本也行的。
百度地图api手机端定位及加载实时路况信息,移动定位,浏览器定位,实时路况
将百度地图嵌入c# cs窗体中,cs...查了很多资料,参考网上众多朋友的代码及百度地图API demo,这个例子中的js代码不用key,方便很多,使用的是v1.4版,少了很多限制,使用.net4.5开发的,可以将c#代码转为低版本也行的。
jQuery百度地图API美团外卖配送地址定位代码,省市区得根据自身需求去增加。
百度地图API是一套为开发者免费提供的基于百度地图的应用程序接口,包括JavaScript、iOS、Andriod、静态地图、Web服务等多种版本,提供基本地图、位置搜索、周边搜索、公交...
百度地图的api文档
测试整合百度地图API,通过Ajax方式读取数据,实现定位。(数据库自行建立)
调用百度地图接口 可供开发者使用 资源非常得不错 希望能帮到你
百度地图API使用的简单介绍的系列博客2-显示百度地图在手机界面上面
百度地图 API 自定义地图底层 WEB 版本 用于用户自定义地图的底层
本离线开发包,支持百度地图离线开发。用于无网络环境下的百度地图二次开发。 包含以下文件: 1,基础API:apiv1.3.min.js ,有注释可参考; 2,基础CSS: bmap.css ; 3,依赖文件:map,oppc,tile,control,marker; 4...
Qt5实现百度地图API的调用——物体轨迹的设计、插入、删除、保存、读取。使用js文件实现Qt和API之间的沟通。https://mp.csdn.net/editor/html/114036591中有项目效果运行截图和简要的说明,欢迎交流。
百度地图API实例百度地图API实例百度地图API实例百度地图API实例百度地图API实例百度地图API实例百度地图API实例
通过调用百度地图API咋子前台页面展示自己的地理位置信息并且如数据库 ,通过日期查询,连成轨迹。
需要用百度地图定位,找了N个不能用的版本后,终于总结出了可以正常使用的代码。 =====百度地图API(商铺定位)开发实例===== 本实例可应用于做带有商家地图定位的程序: 下载实例后,你可以做的是: 1.在地图中...
python调用百度地图API实现经纬度换算、热力地图全流程指南
使用javascript 调用百度地图api 调用百度地图api 非常简单
使用checkip获取IP,使用百度地图api用获取到的IP获取经纬度以及大致城市,最后使用百度地图api获取详细地址