当前位置: 代码迷 >> Android >> Android-高德map
  详细解决方案

Android-高德map

热度:44   发布时间:2016-04-28 01:13:15.0
Android-高德地图
配置信息
--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" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.WRITE_SETTINGS" /><meta-data        android:name="com.amap.api.v2.apikey"        android:value="8367898f3fff604f75d81701e24e85ca" />

1.基本地图显示
MapView:定义地图容器
AMap:地图控制 包括显示交通 定位层显示配置
>初始化容器MapView
>配置信息

private void init() {		if (aMap == null) {			aMap = mapView.getMap();		}	}

2.通过AMap显示交通
aMap.setTrafficEnabled(true);

3.Location定位功能
>设置当前位置的配置

private void setUpMap() {	MyLocationStyle locationStyle=new MyLocationStyle();	locationStyle.strokeColor(Color.BLACK);	locationStyle.strokeWidth(1.0f);	locationStyle.radiusFillColor(0x8333);	locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker));	aMap.setMyLocationStyle(locationStyle);//设置定位点样式	aMap.setLocationSource(this);//设置监听	aMap.setMyLocationEnabled(true);//启动定位图层}

>继承LocationSource,定位监听开始和结束
@Overridepublic void activate(OnLocationChangedListener arg0) {	mLocationChangedListener=arg0;	if (mLocationManager==null) {		mLocationManager=LocationManagerProxy.getInstance(this);		//调用控制 每2秒更新1次,其主要的实时更改在最后一个参数AMapLocationListener		mLocationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, this);	}}@Overridepublic void deactivate() {	mLocationChangedListener=null;	if (mLocationManager!=null) {		mLocationManager.removeUpdates(this);		mLocationManager.destroy();		mLocationManager=null;	}}

>继承AMapLocationListener,主要实现onLocationChanged修改位置
@Overridepublic void onLocationChanged(AMapLocation arg0) {	if (mLocationChangedListener!=null && arg0!=null) {		mLocationChangedListener.onLocationChanged(arg0);	}}

4.定位中 实时更改方向和位置,默认的定位只是简单的图片 没有涉及方向等(思路:添加标记并用传感器修改指示方向)
>初始化传感器

mSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

>初始化Marker,
private void setUpMap() {		mGPSMarker=aMap.addMarker(new MarkerOptions()		.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker))		.anchor(0.5f, 0.5f));		aMap.setLocationSource(this);	aMap.setMyLocationEnabled(true);}

>启动位置监听器时注册传感器  关闭时注销
@Overridepublic void activate(OnLocationChangedListener arg0) {	//....	mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);}@Overridepublic void deactivate() {	//.....	mSensorManager.unregisterListener(this, mSensor);}

>位置移动时修改Marker
@Overridepublic void onLocationChanged(AMapLocation arg0) {	if (mLocationChangedListener!=null && arg0!=null) {		mLocationChangedListener.onLocationChanged(arg0);		mGPSMarker.setPosition(new LatLng(arg0.getLatitude(),arg0.getLongitude()));	}}

>位置角度转换时修改Marker
@Overridepublic void onSensorChanged(SensorEvent event) {	if (System.currentTimeMillis() - lastTime < TIME_SENSOR) {		return;	}	switch (event.sensor.getType()) {	case Sensor.TYPE_ORIENTATION: {		float x = event.values[0];		 		x += getScreenRotationOnPhone(this);		x %= 360.0F;		if (x > 180.0F)			x -= 360.0F;		else if (x < -180.0F)			x += 360.0F;		if (Math.abs(mAngle -90+ x) < 3.0f) {			break;		}		mAngle = x;		if (mGPSMarker != null) {			mGPSMarker.setRotateAngle(-mAngle); 			aMap.invalidate();		}		lastTime = System.currentTimeMillis();	}	}}/** * 获取当前屏幕旋转角度 *  * @param activity * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏 */public static int getScreenRotationOnPhone(Context context) {	final Display display = ((WindowManager) context			.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();	switch (display.getRotation()) {		case Surface.ROTATION_0:			return 0;		case Surface.ROTATION_90:			return 90;		case Surface.ROTATION_180:			return 180;		case Surface.ROTATION_270:			return -90;	}	return 0;}

5.设置缩放和显示位置
LatLngBounds bounds=new LatLngBounds.Builder()				.include(new LatLng(22.117719,112.323274))				.include(new LatLng(22.117719,114.323274))				.include(new LatLng(24.117719,112.323274))				.include(new LatLng(24.117719,114.323274))				.build();aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));aMap.moveCamera(CameraUpdateFactory.zoomBy(8));aMap.setOnMapLoadedListener(this);//设置地图显示监听器

6.设置标记
ArrayList<BitmapDescriptor> icons=new ArrayList<BitmapDescriptor>();icons.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));//...	aMap.addMarker(new MarkerOptions().icons(icons)//显示多图片		.period(2)//变化周期		.position(new LatLng(23.118674,113.321614))		.anchor(0.5f, 0.5f)//锚点位置		.title("fuliyinkai")//点击显示文字		).showInfoWindow();//默认显示aMap.setInfoWindowAdapter(this);//点击标记 显示自定义浮动View

7.设置圆形
aMap.addCircle(	new CircleOptions().center(new LatLng(23.118674,113.321614))	.fillColor(0x88888888).strokeColor(Color.BLACK)	.strokeWidth(5).radius(400));

8.获取周围关键字查询
>文字查找

Inputtips inputtips=new Inputtips(this,new InputtipsListener() {			@Override			public void onGetInputtips(List<Tip> tips, int arg1) {				tip.getName()//获取匹配的单个数据名字			}		});try {	inputtips.requestInputtips(content,"");} catch (AMapException e) {	e.printStackTrace();}

>地图获取相关位置
// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)query = new PoiSearch.Query(keyWord, "", editCity.getText().toString());query.setPageSize(10);// 设置每页最多返回多少条poiitemquery.setPageNum(currentPage);// 设置查第一页poiSearch = new PoiSearch(this, query);poiSearch.setOnPoiSearchListener(this);//主要执行onPoiSearched()poiSearch.searchPOIAsyn();@Overridepublic void onPoiSearched(PoiResult result, int rCode) {	dissmissProgressDialog();// 隐藏对话框	if (rCode == 0) {		if (result != null && result.getQuery() != null) {// 搜索poi的结果			if (result.getQuery().equals(query)) {// 是否是同一条				poiResult = result;				// 取得搜索到的poiitems有多少页				List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始				List<SuggestionCity> suggestionCities = poiResult						.getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息				if (poiItems != null && poiItems.size() > 0) {					aMap.clear();// 清理之前的图标					PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems);					poiOverlay.removeFromMap();					poiOverlay.addToMap();					poiOverlay.zoomToSpan();				} else if (suggestionCities != null						&& suggestionCities.size() > 0) {					showSuggestCity(suggestionCities);				} else {					ToastUtil.show(PoiKeywordSearchActivity.this,							R.string.no_result);				}			}		} else {			ToastUtil.show(PoiKeywordSearchActivity.this,					R.string.no_result);		}	} else if (rCode == 27) {		ToastUtil.show(PoiKeywordSearchActivity.this,				R.string.error_network);	} else if (rCode == 32) {		ToastUtil.show(PoiKeywordSearchActivity.this, R.string.error_key);	} else {		ToastUtil.show(PoiKeywordSearchActivity.this,				getString(R.string.error_other) + rCode);	}}


9.查询route

		FromAndTo fromAndTo=new FromAndTo(mCurrenLocationPoint, latLonPoint);		// 第一个参数表示路径规划的起点和终点,第二个参数表示驾车模式,第三个参数表示途经点,第四个参数表示避让区域,第五个参数表示避让道路		DriveRouteQuery query = new DriveRouteQuery(fromAndTo, RouteSearch.DrivingDefault,				null, null, "");		routeSearch=new RouteSearch(this);		routeSearch.setRouteSearchListener(this);		routeSearch.calculateDriveRouteAsyn(query);// 异步路径规划驾车模式查询
>route监听器

	@Override	public void onBusRouteSearched(BusRouteResult arg0, int arg1) {	}	@Override	public void onDriveRouteSearched(DriveRouteResult result, int rCode) {		if (rCode != 0) {			return;		}		if (result != null && result.getPaths() != null				&& result.getPaths().size() > 0) {			driveRouteResult = result;			DrivePath drivePath = driveRouteResult.getPaths().get(0);			aMap.clear();// 清理地图上的所有覆盖物			DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(					this, aMap, drivePath, driveRouteResult.getStartPos(),					driveRouteResult.getTargetPos());			drivingRouteOverlay.removeFromMap();			drivingRouteOverlay.addToMap();			drivingRouteOverlay.zoomToSpan();		}					}	@Override	public void onWalkRouteSearched(WalkRouteResult arg0, int arg1) {	}



  相关解决方案