高德地图的使用
1.下载相关的包,添加到lib中
2.申请成为高德开发者,添加应用获取key
3.将key添加到应用中<meta-data android:name="com.amap.api.v2.apikey" android:value="请输入您的用户Key"></meta-data>
4.添加权限
5.布局添加地图控件
6.在Activity中拿到控件设置监听,进行调用地图的相关功能
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.amap.api.maps.MapView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/mv_mapview"
></com.amap.api.maps.MapView>
<Button
android:id="@+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重新定位"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="150dp"
android:layout_marginBottom="10dp">
</Button>
</RelativeLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<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.WAKE_LOCK" />
<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="75e65da11ab8a6f9940e1184840440ba"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PoiKeywordSearchActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".PoiSerach"
android:label="@string/app_name">
</activity>
<service android:name="com.amap.api.location.APSService"></service>
</application>
</manifest>
Activity类
public class MainActivity extends Activity implements LocationSource,
AMapLocationListener, OnCheckedChangeListener, OnPoiSearchListener,
OnPOIClickListener, OnMarkerClickListener,InfoWindowAdapter,OnInfoWindowClickListener {
private MapView mv_mapview;
private AMap mAMap;
private AMapLocationClient mLocationClient = null;
private AMapLocationClientOption mLocationOption = null;
private Button tv_location;
private int currentPage = 0;// 规定当前展示的是搜索出来的第几页数据
//private LatLonPoint lp;
private PoiSearch.Query query;
private PoiSearch poiSearch;// POI搜索
@Override
protected void 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
public void onClick(View arg0) {
setLocation();
}
});
setLocation();
}
public void 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
public void onCheckedChanged(RadioGroup arg0, int arg1) {
}
// 当定位改变时执行的方法.参数是我定位的位置信息
@Override
public void 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);
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是弹出信息下面的信息
public void addMyMark(AMapLocation amapLocation,String title,String snippet){
Map<String,String> doctorMap=new HashMap<String, String>();
// 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(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()))
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED))).setObject(doctorMap);//这是对地图添加标记//显示的是红色标记//HUE_AZURE默认的标记颜色
}
@Override
public void activate(OnLocationChangedListener arg0) {
}
@Override
public void deactivate() {
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stopLocation();
mLocationClient.onDestroy();
mv_mapview.onDestroy();
}
@Override
public void onPoiItemSearched(PoiItem arg0, int arg1) {
}
private PoiResult poiResult;
// 当poi搜索结束后执行的方法
@Override
public void onPoiSearched(PoiResult result, int arg1) {
//获取成功后返回1000:具体的错误返回码查看高德api
if (arg1 == 1000) {
if (result.getQuery().equals(query)) {
poiResult = result;
// 取得搜索到的poiitems有多少页
List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0�?��
if (poiItems != null && poiItems.size() > 0) {
// 设置多个医疗点点
PoiOverlay poiOverlay = new PoiOverlay(mAMap, poiItems);
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
}
} else {
Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_LONG)
.show();
}
}
}
// 当标记点点击后执行的方法//poi结果自动添加了标记
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();//设置点击标记后显示信息框
return false;
}
// 点击地图上通过poi搜索出来的地点,添加到地图的标记中
@Override
public void onPOIClick(Poi poi) {
}
@Override
protected void onPause() {
super.onPause();
mv_mapview.onPause();
}
@Override
protected void onResume() {
super.onResume();
mv_mapview.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mv_mapview.onSaveInstanceState(outState);
}
//标记点击后展示的信息的点击的监听
@Override
public void onInfoWindowClick(Marker marker) {
@SuppressWarnings("unchecked")
Map<String,String> doctorInfo= (Map<String, String>) 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
return null;
}
}