个人感觉高德的sdk比百度的好用,而且出错概率也更小,如果用高德地图做一个简单的当前地点定位,不仅不需要设置mapView控件,直接集成在activity里即可。而且还可以按照固定的刷新频率获得经纬度,以及当前所在的街道和建筑物的名字,代码不长,本质上给LocationMangerProxy配置一个AmapLocationLister监听器即可,可以配置在任意一个activity或service中,百度地图换了新版之后旧版的类就没有了,真是太难用了
配置环境
首先去官网下载sdk和demo
下载完后把左边开发包里的jar包和.so文件放到lib目录下,
把demo包里的AMap_Location_v1.4.0.jar也放进去,放完后是这样的
千万注意:AMap_3DMap_v2.6.0_20150914.jar要放在lib文件夹中,上图没有表示出来
最后把从官网申请的key写在manefest中
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="42af3812644d8c1774dba300630980a8"/>
1、任意activity获取当前经纬度的方法
package com.example.qichebaojiata;
import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
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.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;
public class MapActivity extends Activity implements AMapLocationListener {//继承位置监听器,有一个最重要的方法,onLocationChanged(AMapLocation arg0)用于时时刷新当前位置
//声明变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化定位对象,LocationManagerProxy类是个位置管理类,给它设置一个回调监听器,指定定位设备,指定刷新频率等就可以用了
LocationManagerProxy mLocationManagerProxy =LocationManagerProxy .getInstance(this);//定位对象与监听器绑定
//注册定位监听
mLocationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork,//定位设备,netWork是全部都用,
1000,//刷新频率,单位ms
15, //移动距离后刷新,仅对gps有效
this//AMapLocationListener 回调监听器
);
}
@Override
/**
* 定位回调
*/
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
//检测错误码
if(arg0!=null&&arg0.getAMapException().getErrorCode()==0){//等于0是成功了
System.out.println("城市:"+arg0.getCity()+"电话区号"+arg0.getCityCode());
System.out.println("省:"+arg0.getProvince());
System.out.println("国家:"+arg0.getCountry());
System.out.println("经度"+arg0.getLatitude());
System.out.println("纬度"+arg0.getLongitude());
System.out.println("路是:"+arg0.getRoad());
System.out.println("速度:"+arg0.getSpeed());
System.out.println("楼层:"+arg0.getFloor());
System.out.println("建筑物"+arg0.getPoiName());
}else {
System.out.println("定位失败");
}
}
}
附近位置搜索功能:
先让activity继承一个onPoiSearchListener
public class MapActivity extends Activity implements AMapLocationListener, OnPoiSearchListener
在activity中写一个方法
public void search(){
//Query类是一个搜索条件的封装类,用于封装页数和每页多少个
PoiSearch.Query query = new PoiSearch.Query("车","汽车服务|汽车销售|汽车维修|摩托车服务|交通设施服务|道路附属设施","青岛市");
// 第一个参数表示搜索字符串,
//第二个参数表示POI搜索类型,二者选填其一,
//POI搜索类型共分为以下20种:汽车服务|汽车销售|
//汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|
//住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|
//金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
//cityCode表示POI搜索区域的编码,是必须设置参数
query.setPageSize(10);//设置每页返回多少条
query.setPageNum(0);//设置车讯页码
//PoiSearch类是一个搜索引擎,如果要返回搜索结果需要绑定一个OnPoiSearchListener类
PoiSearch poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);//设置本类为搜索监听类
//设置一个地图点,LatLonPoint就是一个经纬度的封装类
LatLonPoint point = new LatLonPoint(amapLocation.getLatitude(), amapLocation.getLongitude());
//地点对象LatLonPoint如果想和引擎poiSearch设置进去,要用另一个封装类SearchBound
//它封装了当前位置,搜索半径
poiSearch.setBound(new SearchBound(point,2000,true));//第二个参数是搜索半径
poiSearch.searchPOIAsyn();
}
这个search()方法需要在获得了当前经纬度之后调用
AMapLocation amapLocation;
boolean searchFlag = false;
@Override
/**
* 定位回调
*/
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
//检测错误码
if(arg0!=null&&arg0.getAMapException().getErrorCode()==0){//等于0是成功了
amapLocation = arg0;
if(!searchFlag){
search();
searchFlag=true;
}
}else {
System.out.println("定位失败");
}
}
写一个专门用来定位的服务也很简单
package com.situ.service;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
public class GPSService extends Service implements AMapLocationListener {
public static String PROVENCE = "山东";
public static String CITY_NAME = "青岛";
public static double LATITUDE = 0.0;
public static double LONGITUDE = 0.0;
public static String POI_NAME = "未知";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("服务启动");
System.out.println("服务启动");
System.out.println("服务启动");
System.out.println("服务启动");
//初始化定位对象,LocationManagerProxy类是个位置管理类,给它设置一个回调监听器,指定定位设备,指定刷新频率等就可以用了
LocationManagerProxy mLocationManagerProxy =LocationManagerProxy .getInstance(getApplicationContext());//定位对象与监听器绑定
//注册定位监听
mLocationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork,//定位设备,netWork是全部都用,
2000,//刷新频率,单位ms
15, //移动距离后刷新,仅对gps有效
this//AMapLocationListener 回调监听器
);
return super.onStartCommand(intent, flags, startId);
}
@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
//检测错误码
if(arg0!=null&&arg0.getAMapException().getErrorCode()==0){//等于0是成功了
System.out.println("城市:"+arg0.getCity()+"电话区号"+arg0.getCityCode());
this.PROVENCE = arg0.getProvince();
this.CITY_NAME = arg0.getCity();
this.POI_NAME = arg0.getPoiName();
this.LATITUDE = arg0.getLatitude();
this.LONGITUDE = arg0.getLongitude();
}else {
System.out.println("定位失败");
}
}
}
我就喜欢把AmapLocationListener放在一个service里,实现监听器的分离,那么在activity里的map对象想获得当前定位的蓝色指针,就要外接listener,这样activity就不需要继承
AmapLocationListener接口了,可以实现资源利用的优化
package com.situ.schoolsale;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
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.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.situ.service.GPSService;
public class MapActivity extends Activity implements LocationSource{//地图加载完成的监听器
//声明变量
private MapView mapView;
private AMap aMap;
LocationManagerProxy mAMapLocationManager ;
OnLocationChangedListener mListener;
private Marker marker;// 定位雷达小图标
List<LatLng> list = new ArrayList<LatLng>();//用于存储轨迹的单点
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
if(GPSService.isRunning==false){
startService(new Intent(this,GPSService.class));//开启GPS服务
}
//在onCreat方法中给aMap对象赋值
//显示地图用的三句话
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(save