您的位置:首页 > 其它

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

2015-08-16 11:04 567 查看
跟集成百度地图一样,首先获取KEY,获取方式(官方的截图)



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

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

1.从网站下载并解压得到定位包“Android_Location_V1.xx.jar“。

2.开发工程中新建“libs”文件夹,将定位包拷贝到 libs 的根目录下。拷贝完成后的工程目录(以 V1.0.4 为例)如图所示:

image

注意:若您在 Eclipse 上使用 adt22 版本插件,则需要在 Eclipse 上进行如下配置:

选中 Eclipse 的工程,右键选择 “Properties > Java Build Path > Order and Export”,勾选 “Android Private Libraries”。

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" />

<activity android:name="com.amap.demo.LocationManager" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
4.添加权限。在工程的“AndroidManifest.xml”文件中进行添加,请直接拷贝。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
5.clean 工程,结束配置。


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

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

先看自己的配置文件

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/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>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

</manifest>


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

package com.example.helloworld;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

//实现定位接口
public class MainActivity extends Activity implements AMapLocationListener {
// 初始化定位对象
LocationManagerProxy mLocationManagerProxy;

protected void onCreate(Bundle savedInstanceState) {
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
protected void onResume() {
super.onResume();
}

//生命周期的管理
@Override
protected void onDestroy() {
super.onDestroy();
mLocationManagerProxy.destroy();
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

// 处理定位结果
@Override
public void onLocationChanged(AMapLocation arg0) {
// 定位回调--getErrorCode() == 0说明正确定位返回了
if (arg0 != null && arg0.getAMapException().getErrorCode() == 0) {
Log.e("helloworld", arg0.toString());
}
}
}


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

package com.example.helloworld;

import java.util.List;

import com.amap.api.location.AMapLocalDayWeatherForecast;
import com.amap.api.location.AMapLocalWeatherForecast;
import com.amap.api.location.AMapLocalWeatherListener;
import com.amap.api.location.AMapLocalWeatherLive;
import com.amap.api.location.LocationManagerProxy;

import android.app.Activity;
import android.util.Log;

//实现天气回调接口
public class WeatherActivity extends Activity implements AMapLocalWeatherListener{
LocationManagerProxy mLocationManagerProxy;
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化定位对象
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
//注册天气监听
mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_FORECAST, this);
};

protected void onDestroy() {
super.onDestroy();
}

@Override
public void onWeatherForecaseSearched(AMapLocalWeatherForecast arg0) {
//未来天气预报
List<AMapLocalDayWeatherForecast> list= arg0.getWeatherForecast();
for(int i =0;i<list.size();i++){
AMapLocalDayWeatherForecast dayWeather = list.get(i);
Log.e("helloworld", "城市:"+dayWeather.getCity());
Log.e("helloworld", "时间:"+dayWeather.getDate());
Log.e("helloworld", "温度:"+dayWeather.getDayTemp());
Log.e("helloworld", "风力:"+dayWeather.getDayWindPower());
}
}

@Override
public void onWeatherLiveSearched(AMapLocalWeatherLive arg0) {
//当天天气预报
Log.e("helloworld", "城市:"+arg0.getCity());
Log.e("helloworld", "温度:"+arg0.getTemperature());
Log.e("helloworld", "风力:"+arg0.getWindPower());
};
}


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

package com.example.helloworld;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

/**
* 案例效果: 点击地图mapView,会出现一个圆圈, 在通过AMapLocationListener定位功能,获取经度维度
* 通过mLocationManagerProxy.addGeoFenceAlert进行设置在定位的经度,纬度进行半径设置,
* 也就是进行围栏效果,如果一旦出所警戒的区域,会激发异步intent,接受广播
* if (i == 1) { Log.e("helloworld",
* "在地理围栏内部"); } if (i == 0) { Log.e("helloworld", "在地理围栏外面"); }
*
*/
public class GeoFenceActivity extends Activity implements OnMapClickListener,
AMapLocationListener {
MapView mapView;
LocationManagerProxy mLocationManagerProxy;
String GEOFENCE_BROADCAST_ACTION = "com.example.helloworld";
PendingIntent mPendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
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);
// 构建地理围栏广播
Intent intent = new Intent(GEOFENCE_BROADCAST_ACTION);
/**
* getActivity()的意思其实是,获取一个PendingIntent对象,而且该对象日后激发时所做的事情是启动一个新activity
* 。也就是说,当它异步激发时,会执行类似Context.startActivity()那样的动作。
*/
mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0,
intent, 0);
// 接受怎样的广播--设置下
IntentFilter intentfilter = new IntentFilter();
intentfilter.addAction(GEOFENCE_BROADCAST_ACTION);
// 注册广播!!!
this.registerReceiver(mGeoFenceReceiver, intentfilter);
// 注册定位监听
mLocationManagerProxy.requestLocationData(LocationManager.GPS_PROVIDER,
2000, 15, this);

}

// 定义一个广播接收器
private BroadcastReceiver mGeoFenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int i = intent.getIntExtra("status", -1);
Log.e("helloworld", "收到广播");
if (i == 1) {
Log.e("helloworld", "在地理围栏内部");
}
if (i == 0) {
Log.e("helloworld", "在地理围栏外面");
}

}
};

@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onDestroy() {
super.onDestroy();
}

/**
* 实现OnMapClickListener的监听回调方法
*/
@Override
public void onMapClick(LatLng arg0) {
Log.e("helloworld", "lat = " + arg0.latitude);
Log.e("helloworld", "lon = " + arg0.longitude);
// 点击地图时候,在地图上添加一个围栏圆圈,设置半径1000米
mapView.getMap().addCircle(
new CircleOptions().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
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub

}
}


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

package com.example.helloworld;

import java.util.List;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.MapView;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiItemDetail;
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 android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 功能描述
* 启动后加载mapView地图界面
* 视图中又一个button,button是用来点击后,搜索自己位置附近的北京的KFC快餐店的
* 点击后,会log出定位某处的附近2000圈内的快餐店即可
*
* 实现方案:
* 获取mapview控件,初始化,让其展现地图界面
* 注册定位监听,让其可以实现定位功能,因为要搜索kfc
* 在button进行点击后进行的操作是
* 创建搜索实例对象,并且设置搜索区域范围,然后实现搜索监听,
* 在回调方法中进行处理搜索到的结果即可
*
*/
public class SearchActivity extends Activity implements AMapLocationListener,OnPoiSearchListener,OnClickListener{
MapView mapview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取mapView控件
mapview = (MapView)findViewById(R.id.main_mapView);
//初始化
mapview.onCreate(savedInstanceState);
//获取定位实例
LocationManagerProxy mLocationManagerProxy = LocationManagerProxy.getInstance(this);
//注册定位监听--传递定位方式,时间,距离
mLocationManagerProxy.requestLocationData(
LocationManager.GPS_PROVIDER,2000, 15, this);
//鼠标的作用是,点击button进行获取定位
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
mapview.onResume();
}

@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void onLocationChanged(Location location) {

}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}
@Override
public void onProviderEnabled(String provider) {

}
@Override
public void onProviderDisabled(String provider) {

}
/**
* 实现AMapLocationListener的回调方法,会打印出地理位置
*/
AMapLocation location;
@Override
public void onLocationChanged(AMapLocation arg0) {
location = arg0;
Log.e("helloworld", arg0.toString());
}
@Override
public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {

}
/**
* 实现OnPoiSearchListener搜索的的回调方法
*/
@Override
public void onPoiSearched(PoiResult arg0, int arg1) {
/**
* 如果是arg返回0,即搜索成功
* 遍历搜索结果打印相信信息即可
*/
if(arg1 == 0 ){
List<PoiItem> list = arg0.getPois();
for(int i = 0;i<list.size();i++){
PoiItem item = list.get(i);
Log.e("helloworld", item.toString());
}

}
}

public void search(){
//高德搜索功能
//1、创建要搜素的内容,即类似sql语句
PoiSearch.Query query = new PoiSearch.Query("kfc","餐饮","北京市");
query.setPageSize(10);  // 设置每页最多返回多少条poiitem
query.setPageNum(0);//第一页
//2、创建搜索的实例对象
PoiSearch poiSearch = new PoiSearch(this, query);//context,query
//3、设置搜索监听,处理搜索结果即可
poiSearch.setOnPoiSearchListener(this);
//创建经纬度点位
LatLonPoint poiont = new LatLonPoint(location.getLatitude(),location.getLongitude());
//设置搜索区域--根据给定的参数来构造PoiSearch.SearchBound 的新对象,默认由近到远排序。
//SearchBound(LatLonPoint center, int radiusInMeters, boolean isDistanceSort)
poiSearch.setBound(new SearchBound(poiont, 2000, true));
//执行搜索
poiSearch.searchPOIAsyn();
}
/**
* button的点击事件
*/
@Override
public void onClick(View v) {
search();
}
}


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

package com.example.helloworld;

import java.util.ArrayList;
import java.util.List;

import com.amap.api.maps.AMap.OnMapLoadedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolylineOptions;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

/**
* 功能描述:
* 这个是实现准备一系列的轨迹的经度维度的数组数据
* 实现监听setOnMapLoadedListener,即地图一旦加载就执行该回调方法,
* 并且添加有轨迹点组成的轨迹路线即可
* 	public void onMapLoaded() {
mapview.getMap().addPolyline(new PolylineOptions().addAll(list));
}
*
*/
public class TraceActivity extends Activity implements OnMapLoadedListener{
MapView mapview;
double trace[] = {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
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapview = (MapView)findViewById(R.id.main_mapView);
mapview.onCreate(savedInstanceState);
//地图一旦加载的产生监听
mapview.getMap().setOnMapLoadedListener(this);
//创建经度维度点的集合对象
list = new ArrayList<LatLng>();
for(int i=0;i<trace.length-1;i++){
LatLng  latlng = new LatLng(trace[i],trace[++i]);
list.add(latlng);
}

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mapview.onResume();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

/**
* 地图一旦加载就绘制轨迹
*/
@Override
public void onMapLoaded() {
mapview.getMap().addPolyline(new PolylineOptions().addAll(list));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: