高德地图的实现小结

1.注册高德账号
2.拿到项目的shal1分为调试版()和发布版():通过window中的preference中的android 中的build
3.在自己的高德账号中添加项目key
4.在清单文件中添加
android:name="com.amap.api.v2.apikey"
android:value="https://www.it610.com/article/75e65da11ab8a6f9940e1184840440ba"/>
5.添加权限










6.导包根据提示导入适合的包
7.代码
(1)布局中添加
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/mv_mapview"
>
(2)activity中
mv_mapview=(MapView)findViewById(R.id.mv_mapview);
mv_mapview.onCreate(savedInstanceState);
mAMap=mv_mapview.getMap();
mAMap.setLocationSource(this); //设置地图位置的监听,有回调,可以获取用户的具体位置信息
mAMap.setMyLocationEnabled(true); //以上是设置地图的相关属性,以及拿到地图类AMap对象

//设置开始定位的参数
mLocationClient = newAMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(this);
mLocationOption = newAMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
mLocationOption.setNeedAddress(true);
mLocationOption.setInterval(5*60*1000); //
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();

记得定位结束后关闭定位
mLocationClient.stopLocation();
mLocationClient.onDestroy();

public voidonLocationChanged(AMapLocation amapLocation) {}
//以上是定位的内容
//下面是poi关键字搜索
//amapLocation是位置参数是上面定位后获取到的
query = newPoiSearch.Query("医疗","", amapLocation.getCity()); //第一个参数表示搜索字符串,第二个参数表示poi搜索类型(可以为空),第三个参数表示poi搜索区域(空字符串代表全国)
query.setPageSize(100);
query.setPageNum(currentPage); //必须从0开始
poiSearch = newPoiSearch(this,query);
poiSearch.setBound(new SearchBound(newLatLonPoint(amapLocation.getLatitude(),
amapLocation.getLongitude()),800,true)); //设置poi搜的中心点和中心点周围的范围
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();

//poi回调:可以显示地图
public voidonPoiSearched(PoiResult result, int arg1) {
if(arg1==1000){
if(result.getQuery().equals(query)) {
poiResult = result; //搜索结果
// 取得搜索到的poiitems有多少页
List poiItems =poiResult.getPois(); // 取得第一页的poiitem数据,页数从数字0开始
if (poiItems != null &&poiItems.size() > 0) {
//设置多个医疗点点
PoiOverlay poiOverlay = newPoiOverlay(mAMap, poiItems);
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan(); }
}else{
Toast.makeText(getApplicationContext(),"获取poi失败",Toast.LENGTH_LONG).show(); }}}


高德地图显示我的位置和周边搜索的实现实例代码:
publicclass MainActivity extends Activity implements LocationSource,
AMapLocationListener,OnCheckedChangeListener, OnPoiSearchListener,
OnPOIClickListener, OnMarkerClickListener,InfoWindowAdapter,OnInfoWindowClickListener{
private MapViewmv_mapview;
private AMapmAMap;
private AMapLocationClient mLocationClient = null;
private AMapLocationClientOption mLocationOption = null;
private Buttontv_location;
privateint currentPage = 0; // 规定当前展示的是搜索出来的第几页数据
//privateLatLonPoint lp;
private PoiSearch.Query query;
private PoiSearchpoiSearch; // POI搜索

@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mv_mapview = (MapView) findViewById(R.id.mv_mapview);
mv_mapview.onCreate(savedInstanceState);
mAMap =mv_mapview.getMap();
mAMap.setLocationSource(this);
mAMap.setMyLocationEnabled(true); //设置获取位置的监听
mAMap.setOnMarkerClickListener(this); //设置点击标记的监听
mAMap.setOnInfoWindowClickListener(this); //设置点击信息的监听
tv_location=(Button) findViewById(R.id.tv_location);
tv_location.setOnClickListener(new View.OnClickListener(){

@Override
publicvoid onClick(View arg0) {
setLocation();
}

});
setLocation();
}

publicvoid setLocation() {
// 定位
mLocationClient = new AMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(this);
mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
mLocationOption.setNeedAddress(true);
mLocationOption.setInterval(5 * 60 * 1000); //
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}

@Override
publicvoid onCheckedChanged(RadioGroup arg0, int arg1) {
}

// 当定位改变时执行的方法.参数是我定位的位置信息
@Override
publicvoid onLocationChanged(AMapLocation amapLocation){
if (amapLocation != null && amapLocation.getErrorCode() == 0){
mAMap.clear(); // 清空地图
// 添加我的位置的标记//地图中的poi搜索会自动添加标记//我们也可以添加自己的标记,如下:标记的具体点击效果查看demo中的mark
addMyMark(amapLocation,"我的位置","这是我自己添加的我的位置");
// poi搜索开始
query = new PoiSearch.Query("医疗", "",amapLocation.getCity());
query.setPageSize(100);
query.setPageNum(currentPage); //必须从0开始
poiSearch = new PoiSearch(this, query);
poiSearch.setBound(new SearchBound(new LatLonPoint(amapLocation
.getLatitude(),amapLocation.getLongitude()), 800,true)); // 设置poi搜的中心点和中心点周围的范围
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
} else {
}
}

//添加自己的标记//第一个参数用于获取添加标记时的经纬度,title是点击标记弹出信息是的标题,snippet是弹出信息下面的信息
publicvoid addMyMark(AMapLocation amapLocation,Stringtitle,String snippet){
MapdoctorMap=newHashMap();
//doctorMap.put("doctorid",coordinates.get(i).getAliasAccount());
//doctorMap.put("headimage",coordinates.get(i).getHeadimg());
//doctorMap.put("address",coordinates.get(i).getInHospital());
//doctorMap.put("jid",coordinates.get(i).getJid());
doctorMap.put("mymark","这是我自己添加的标记"); //可以传递过去数据,在点击标记时获取到
mAMap.addMarker(new MarkerOptions()
.position(newLatLng(amapLocation.getLatitude(), amapLocation.getLongitude()))
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED))).setObject(doctorMap); //这是对地图添加标记//显示的是红色标记//HUE_AZURE默认的标记颜色
}
@Override
publicvoid activate(OnLocationChangedListener arg0) {
【高德地图的实现小结】}

@Override
publicvoid deactivate() {

}

@Override
protectedvoid onDestroy() {
super.onDestroy();
mLocationClient.stopLocation();
mLocationClient.onDestroy();
mv_mapview.onDestroy();
}

@Override
publicvoid onPoiItemSearched(PoiItem arg0, int arg1) {

}

private PoiResultpoiResult;

// 当poi搜索结束后执行的方法
@Override
publicvoid onPoiSearched(PoiResult result, int arg1) {
//获取成功后返回1000:具体的错误返回码查看高德api
if (arg1 == 1000) {
if(result.getQuery().equals(query)) {
poiResult = result;
// 取得搜索到的poiitems有多少页
ListpoiItems = poiResult.getPois(); // 取得第一页的poiitem数据,页数从数字0????
if (poiItems != null &&poiItems.size() > 0) {
// 设置多个医疗点点
PoiOverlaypoiOverlay = newPoiOverlay(mAMap,poiItems);
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
}
} else {
Toast.makeText(getApplicationContext(),"失败", Toast.LENGTH_LONG)
.show();
}
}
}

// 当标记点点击后执行的方法//poi结果自动添加了标记
@Override
publicboolean onMarkerClick(Marker marker) {
marker.showInfoWindow(); //设置点击标记后显示信息框
returnfalse;
}

// 点击地图上通过poi搜索出来的地点,添加到地图的标记中
@Override
publicvoid onPOIClick(Poi poi) {
}

@Override
protectedvoid onPause() {
super.onPause();
mv_mapview.onPause();
}

@Override
protectedvoid onResume() {
super.onResume();
mv_mapview.onResume();
}

@Override
protectedvoid onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mv_mapview.onSaveInstanceState(outState);
}

//标记点击后展示的信息的点击的监听
@Override
publicvoid onInfoWindowClick(Marker marker) {
@SuppressWarnings("unchecked")
MapdoctorInfo= (Map) marker.getObject(); //获取object信息//该信息是poi搜索结果自动添加标记时添加的,还有我们自己添加标记时设置的
doctorInfo=doctorInfo;
if(doctorInfo!=null){
Toast.makeText(this, "这是标题:"+marker.getTitle()+"这是我自己添加的标记传过来的值:"+doctorInfo.get("mymark"), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_SHORT).show();
}
}

//设置点击标记后弹出的view布局
@Override
public View getInfoContents(Marker marker){
View view =getLayoutInflater().inflate(R.layout.poikeywordsearch_uri, null);
TextView title = (TextView)view.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView snippet =(TextView) view.findViewById(R.id.snippet);
snippet.setText(marker.getSnippet());
ImageButton button =(ImageButton) view.findViewById(R.id.start_amap_app);
return view;
}

@Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
returnnull;
}
}

    推荐阅读