1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 高德地图之定位篇-----定位 预测天气 围栏 搜索周边 行踪轨迹

高德地图之定位篇-----定位 预测天气 围栏 搜索周边 行踪轨迹

时间:2023-12-23 21:11:35

相关推荐

高德地图之定位篇-----定位 预测天气 围栏 搜索周边 行踪轨迹

/u013210620/article/details/47700629

跟集成百度地图一样,首先获取KEY,获取方式(官方的截图)

这篇主要是讲解高德地图定位篇,高德地图定位篇跟高德地图篇是不同的sdk,分离开了。。。

来看下配置流程吧,配置是第一位的

[java]view plain copy 1.从网站下载并解压得到定位包“Android_Location_V1.xx.jar“。 2.开发工程中新建“libs”文件夹,将定位包拷贝到libs的根目录下。拷贝完成后的工程目录(以V1.0.4为例)如图所示: image 注意:若您在Eclipse上使用adt22版本插件,则需要在Eclipse上进行如下配置: 选中Eclipse的工程,右键选择“Properties>JavaBuildPath>OrderandExport”,勾选“AndroidPrivateLibraries”。 3.添加用户Key。在工程的“AndroidManifest.xml”文件如下代码中添加您的用户Key。 <application android:icon="@drawable/icon" android:label="@string/app_name"> <meta-data android:name="com.amap.api.v2.apikey" android:value="请输入您的用户Key"/> <activityandroid:name="com.amap.demo.LocationManager"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> 4.添加权限。在工程的“AndroidManifest.xml”文件中进行添加,请直接拷贝。 <uses-permissionandroid:name="android.permission.INTERNET"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_CONFIGURATION"/> 5.clean工程,结束配置。

接下来就开始贴自己总结的代码了,,,

*****************************************************华丽的分割线********************************************************

先看自己的配置文件

AndroidManifest.xml

[html]view plain copy <?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="/apk/res/android" package="com.example.helloworld" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <meta-data android:name="com.amap.api.v2.apikey" android:value="c9e78719c6c000407753045aeb0de1fd"/> <!--启动的activity不同--> <activity android:name=".TraceActivity" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-permissionandroid:name="android.permission.INTERNET"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_CONFIGURATION"/> <uses-permissionandroid:name="android.permission.WRITE_SETTINGS"/> </manifest>

MainActivity(先测试下能不能定位,为以后的demo做准备)

[java]view plain copy packagecom.example.helloworld; importcom.amap.api.location.AMapLocation; importcom.amap.api.location.AMapLocationListener; importcom.amap.api.location.LocationManagerProxy; importcom.amap.api.location.LocationProviderProxy; importandroid.app.Activity; importandroid.location.Location; importandroid.os.Bundle; importandroid.util.Log; //实现定位接口 publicclassMainActivityextendsActivityimplementsAMapLocationListener{ //初始化定位对象 LocationManagerProxymLocationManagerProxy; protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化定位对象 mLocationManagerProxy=LocationManagerProxy.getInstance(this); /** *注册定位监听 *LocationProviderProxy.AMapNetwork高德定位(高德定位是混合定位,网络和gps均有, *如果都返回,首选gps定位)第二个参数是定位时间频率(-1是单次定位),移动距离,回调监听 */ mLocationManagerProxy.requestLocationData( LocationProviderProxy.AMapNetwork,-1,15,this); } @Override protectedvoidonResume(){ super.onResume(); } //生命周期的管理 @Override protectedvoidonDestroy(){ super.onDestroy(); mLocationManagerProxy.destroy(); } @Override publicvoidonLocationChanged(Locationlocation){ } @Override publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){ } @Override publicvoidonProviderEnabled(Stringprovider){ } @Override publicvoidonProviderDisabled(Stringprovider){ } //处理定位结果 @Override publicvoidonLocationChanged(AMapLocationarg0){ //定位回调--getErrorCode()==0说明正确定位返回了 if(arg0!=null&&arg0.getAMapException().getErrorCode()==0){ Log.e("helloworld",arg0.toString()); } } }

WeatherActivity(高德也可以测试今天或者未来几天的天气,尼玛感觉好强大啊。。。。。。。。。。。)

[java]view plain copy packagecom.example.helloworld; importjava.util.List; importcom.amap.api.location.AMapLocalDayWeatherForecast; importcom.amap.api.location.AMapLocalWeatherForecast; importcom.amap.api.location.AMapLocalWeatherListener; importcom.amap.api.location.AMapLocalWeatherLive; importcom.amap.api.location.LocationManagerProxy; importandroid.app.Activity; importandroid.util.Log; //实现天气回调接口 publicclassWeatherActivityextendsActivityimplementsAMapLocalWeatherListener{ LocationManagerProxymLocationManagerProxy; protectedvoidonCreate(android.os.BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化定位对象 mLocationManagerProxy=LocationManagerProxy.getInstance(this); //注册天气监听 mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_FORECAST,this); }; protectedvoidonDestroy(){ super.onDestroy(); } @Override publicvoidonWeatherForecaseSearched(AMapLocalWeatherForecastarg0){ //未来天气预报 List<AMapLocalDayWeatherForecast>list=arg0.getWeatherForecast(); for(inti=0;i<list.size();i++){ AMapLocalDayWeatherForecastdayWeather=list.get(i); Log.e("helloworld","城市:"+dayWeather.getCity()); Log.e("helloworld","时间:"+dayWeather.getDate()); Log.e("helloworld","温度:"+dayWeather.getDayTemp()); Log.e("helloworld","风力:"+dayWeather.getDayWindPower()); } } @Override publicvoidonWeatherLiveSearched(AMapLocalWeatherLivearg0){ //当天天气预报 Log.e("helloworld","城市:"+arg0.getCity()); Log.e("helloworld","温度:"+arg0.getTemperature()); Log.e("helloworld","风力:"+arg0.getWindPower()); }; }

GeoFenceActivity(围栏效果--不知道为什么用这个名词。。。。。。。。。。。。。。。。)

[java]view plain copy packagecom.example.helloworld; importcom.amap.api.location.AMapLocation; importcom.amap.api.location.AMapLocationListener; importcom.amap.api.location.LocationManagerProxy; importcom.amap.api.maps.AMap.OnMapClickListener; importcom.amap.api.maps.MapView; importcom.amap.api.maps.model.CircleOptions; importcom.amap.api.maps.model.LatLng; importandroid.app.Activity; importandroid.app.PendingIntent; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.location.Location; importandroid.location.LocationManager; importandroid.os.Bundle; importandroid.util.Log; /** *案例效果:点击地图mapView,会出现一个圆圈,在通过AMapLocationListener定位功能,获取经度维度 *通过mLocationManagerProxy.addGeoFenceAlert进行设置在定位的经度,纬度进行半径设置, *也就是进行围栏效果,如果一旦出所警戒的区域,会激发异步intent,接受广播 *if(i==1){Log.e("helloworld", *"在地理围栏内部");}if(i==0){Log.e("helloworld","在地理围栏外面");} * */ publicclassGeoFenceActivityextendsActivityimplementsOnMapClickListener, AMapLocationListener{ MapViewmapView; LocationManagerProxymLocationManagerProxy; StringGEOFENCE_BROADCAST_ACTION="com.example.helloworld"; PendingIntentmPendingIntent; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件视图id mapView=(MapView)findViewById(R.id.main_mapView); //进行onCreate初始化操作 mapView.onCreate(savedInstanceState); //点击围栏圆圈的监听 mapView.getMap().setOnMapClickListener(this); //构建定位控制类 mLocationManagerProxy=LocationManagerProxy.getInstance(this); //构建地理围栏广播 Intentintent=newIntent(GEOFENCE_BROADCAST_ACTION); /** *getActivity()的意思其实是,获取一个PendingIntent对象,而且该对象日后激发时所做的事情是启动一个新activity *。也就是说,当它异步激发时,会执行类似Context.startActivity()那样的动作。 */ mPendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0, intent,0); //接受怎样的广播--设置下 IntentFilterintentfilter=newIntentFilter(); intentfilter.addAction(GEOFENCE_BROADCAST_ACTION); //注册广播!!! this.registerReceiver(mGeoFenceReceiver,intentfilter); //注册定位监听 mLocationManagerProxy.requestLocationData(LocationManager.GPS_PROVIDER, 2000,15,this); } //定义一个广播接收器 privateBroadcastReceivermGeoFenceReceiver=newBroadcastReceiver(){ @Override publicvoidonReceive(Contextcontext,Intentintent){ inti=intent.getIntExtra("status",-1); Log.e("helloworld","收到广播"); if(i==1){ Log.e("helloworld","在地理围栏内部"); } if(i==0){ Log.e("helloworld","在地理围栏外面"); } } }; @Override protectedvoidonResume(){ super.onResume(); mapView.onResume(); } @Override protectedvoidonDestroy(){ super.onDestroy(); } /** *实现OnMapClickListener的监听回调方法 */ @Override publicvoidonMapClick(LatLngarg0){ Log.e("helloworld","lat="+arg0.latitude); Log.e("helloworld","lon="+arg0.longitude); //点击地图时候,在地图上添加一个围栏圆圈,设置半径1000米 mapView.getMap().addCircle( newCircleOptions().center(arg0).radius(1000)); //根据给定的经纬度和半径,当设备进入或从区域中离开时注册的PendingIntent将被激活一次。此方法需要与定位请求方法同时使用。 mLocationManagerProxy.addGeoFenceAlert(arg0.latitude,arg0.longitude, 1000,1000*60*30,mPendingIntent); /** *latitude-警戒区域中心点的纬度。longitude-警戒区域中心点的经度。radius-警戒区域的半径,单位为米。 *expiration-警戒时间,单位为毫秒,-1表示没有限制。intent *-当检测到进入或离开警戒区域时将被激活的PendingIntent。bundle的status字段,0从区域中离开,1进入该区域。 */ } @Override publicvoidonLocationChanged(Locationlocation){ //TODOAuto-generatedmethodstub } @Override publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){ //TODOAuto-generatedmethodstub } @Override publicvoidonProviderEnabled(Stringprovider){ //TODOAuto-generatedmethodstub } @Override publicvoidonProviderDisabled(Stringprovider){ //TODOAuto-generatedmethodstub } @Override publicvoidonLocationChanged(AMapLocationarg0){ //TODOAuto-generatedmethodstub } }

SearchActivity(搜索周边的kfc,kfc你懂得,不要歪解它的意思。。。。。。。。。。。。。)

[java]view plain copy packagecom.example.helloworld; importjava.util.List; importcom.amap.api.location.AMapLocation; importcom.amap.api.location.AMapLocationListener; importcom.amap.api.location.LocationManagerProxy; importcom.amap.api.location.LocationProviderProxy; importcom.amap.api.maps.MapView; importcom.amap.api.services.core.LatLonPoint; importcom.amap.api.services.core.PoiItem; importcom.amap.api.services.poisearch.PoiItemDetail; importcom.amap.api.services.poisearch.PoiResult; importcom.amap.api.services.poisearch.PoiSearch; importcom.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener; importcom.amap.api.services.poisearch.PoiSearch.SearchBound; importandroid.app.Activity; importandroid.location.Location; importandroid.location.LocationManager; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; /** *功能描述 *启动后加载mapView地图界面 *视图中又一个button,button是用来点击后,搜索自己位置附近的北京的KFC快餐店的 *点击后,会log出定位某处的附近2000圈内的快餐店即可 * *实现方案: *获取mapview控件,初始化,让其展现地图界面 *注册定位监听,让其可以实现定位功能,因为要搜索kfc *在button进行点击后进行的操作是 *创建搜索实例对象,并且设置搜索区域范围,然后实现搜索监听, *在回调方法中进行处理搜索到的结果即可 * */ publicclassSearchActivityextendsActivityimplementsAMapLocationListener,OnPoiSearchListener,OnClickListener{ MapViewmapview; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取mapView控件 mapview=(MapView)findViewById(R.id.main_mapView); //初始化 mapview.onCreate(savedInstanceState); //获取定位实例 LocationManagerProxymLocationManagerProxy=LocationManagerProxy.getInstance(this); //注册定位监听--传递定位方式,时间,距离 mLocationManagerProxy.requestLocationData( LocationManager.GPS_PROVIDER,2000,15,this); //鼠标的作用是,点击button进行获取定位 Buttonbutton=(Button)findViewById(R.id.button); button.setOnClickListener(this); } @Override protectedvoidonResume(){ super.onResume(); mapview.onResume(); } @Override protectedvoidonDestroy(){ super.onDestroy(); } @Override publicvoidonLocationChanged(Locationlocation){ } @Override publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){ } @Override publicvoidonProviderEnabled(Stringprovider){ } @Override publicvoidonProviderDisabled(Stringprovider){ } /** *实现AMapLocationListener的回调方法,会打印出地理位置 */ AMapLocationlocation; @Override publicvoidonLocationChanged(AMapLocationarg0){ location=arg0; Log.e("helloworld",arg0.toString()); } @Override publicvoidonPoiItemDetailSearched(PoiItemDetailarg0,intarg1){ } /** *实现OnPoiSearchListener搜索的的回调方法 */ @Override publicvoidonPoiSearched(PoiResultarg0,intarg1){ /** *如果是arg返回0,即搜索成功 *遍历搜索结果打印相信信息即可 */ if(arg1==0){ List<PoiItem>list=arg0.getPois(); for(inti=0;i<list.size();i++){ PoiItemitem=list.get(i); Log.e("helloworld",item.toString()); } } } publicvoidsearch(){ //高德搜索功能 //1、创建要搜素的内容,即类似sql语句 PoiSearch.Queryquery=newPoiSearch.Query("kfc","餐饮","北京市"); query.setPageSize(10);//设置每页最多返回多少条poiitem query.setPageNum(0);//第一页 //2、创建搜索的实例对象 PoiSearchpoiSearch=newPoiSearch(this,query);//context,query //3、设置搜索监听,处理搜索结果即可 poiSearch.setOnPoiSearchListener(this); //创建经纬度点位 LatLonPointpoiont=newLatLonPoint(location.getLatitude(),location.getLongitude()); //设置搜索区域--根据给定的参数来构造PoiSearch.SearchBound的新对象,默认由近到远排序。 //SearchBound(LatLonPointcenter,intradiusInMeters,booleanisDistanceSort) poiSearch.setBound(newSearchBound(poiont,2000,true)); //执行搜索 poiSearch.searchPOIAsyn(); } /** *button的点击事件 */ @Override publicvoidonClick(Viewv){ search(); } }

TraceActivity(高德也可以对你的行踪进行跟踪,哎,将来可以对付女朋友或者好基友了)

[java]view plain copy packagecom.example.helloworld; importjava.util.ArrayList; importjava.util.List; importcom.amap.api.maps.AMap.OnMapLoadedListener; importcom.amap.api.maps.MapView; importcom.amap.api.maps.model.LatLng; importcom.amap.api.maps.model.PolylineOptions; importandroid.app.Activity; importandroid.os.Bundle; importandroid.util.Log; /** *功能描述: *这个是实现准备一系列的轨迹的经度维度的数组数据 *实现监听setOnMapLoadedListener,即地图一旦加载就执行该回调方法, *并且添加有轨迹点组成的轨迹路线即可 *publicvoidonMapLoaded(){mapview.getMap().addPolyline(newPolylineOptions().addAll(list)); } * */ publicclassTraceActivityextendsActivityimplementsOnMapLoadedListener{ MapViewmapview; doubletrace[]={40.03833763826341,116.44161604271481, 40.038120708755,116.44178901291339, 40.037882375415066,116.4417806669452, 40.03758904414511,116.44176898746878, 40.03744405133165,116.44175898937421, 40.037160730291575,116.44174065740059, 40.03688407626496,116.44172232621654, 40.0366324210887,116.44170566146977, 40.036504105478954,116.4416890116469, 40.03623913323141,116.44166070124143, 40.03601914632045,116.44164404027542, 40.03582913675676,116.44164401726151, 40.035737465479734,116.44164400615833, 40.035550835334085,116.4416123783024, 40.03531757714563,116.44155246627021, 40.035092606829934,116.44152416050346, 40.03497929671584,116.44150418544663, 40.034890970995875,116.44149585752025, 40.03472264127492,116.44148751989984, 40.034544311059626,116.44147918106438, 40.034350992125134,116.44146252316679, 40.03414436321754,116.44142922913052, 40.033942703318765,116.44141756053763, 40.0337594520259,116.44135432702309, 40.03355940132553,116.44138258156585, 40.03336267689871,116.44141582682933, 40.0332193052337,116.44143743434178, 40.03316095218832,116.44144907142875, 40.03307090444962,116.44147900260153, 40.03301251527869,116.44151559133982, 40.032990835497614,116.44152390593369, 40.03295913835145,116.44154386340695, 40.03293078192086,116.44155883094285, 40.03291077610872,116.44156215540048, 40.03285575168102,116.44157711969189, 40.03275240841231,116.44158043405254, 40.03263740996554,116.4415754298076, 40.03257240192978,116.44157874881171, 40.03251239655422,116.44158040498262, 40.03231736762684,116.44159368886524, 40.03210400929456,116.44160364364582, 40.031903970468456,116.44162358064602, 40.031803975210416,116.44161691479444, 40.03160064154208,116.44161023642441, 40.03141064428492,116.44160189623058, 40.03120398679096,116.44158856370204, 40.03100398181874,116.4415852126018, 40.03090898199395,116.44158187421888, 40.030750657801356,116.44157021096972, 40.03068400234474,116.44156022225643, 40.03052737863301,116.44152527100435, 40.03036406140828,116.44150861678068, 40.03021740684516,116.44149529145307, 40.03006407325888,116.44149028254215, 40.02999075078973,116.44148029297878}; List<LatLng>list; @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapview=(MapView)findViewById(R.id.main_mapView); mapview.onCreate(savedInstanceState); //地图一旦加载的产生监听 mapview.getMap().setOnMapLoadedListener(this); //创建经度维度点的集合对象 list=newArrayList<LatLng>(); for(inti=0;i<trace.length-1;i++){ LatLnglatlng=newLatLng(trace[i],trace[++i]); list.add(latlng); } } @Override protectedvoidonResume(){ //TODOAuto-generatedmethodstub super.onResume(); mapview.onResume(); } @Override protectedvoidonDestroy(){ //TODOAuto-generatedmethodstub super.onDestroy(); } /** *地图一旦加载就绘制轨迹 */ @Override publicvoidonMapLoaded(){ mapview.getMap().addPolyline(newPolylineOptions().addAll(list)); } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。