[Android]googleMap的简单使用,地图定位,图标绘制。

    首先项目要设置成支持googlemap的api,在Project Build Target中选择google APIs。

 

    manifest需要加上权限设置。(写在</application>下)

      <uses-permission android:name="android.permission.INTERNET"/> 
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

 

    布局中加上mapview。 apikey需要自己去申请,具体在网上找教程很多。

<com.google.android.maps.MapView
    android:id="@+id/map"  
    android:apiKey="**********************************"      
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"   
    android:clickable="true" />

 

    初始化mapview

 

 

private void initMapView() {
		map = (MapView) findViewById(R.id.map);
		projection = map.getProjection();
		mapControlle = map.getController();
		map.setTraffic(false);// 交通模式
		map.setSatellite(false);// 卫星模式
		map.setBuiltInZoomControls(true);// 打开缩放控件
	}

 

    定位我的位置

 

showLocation(getCurrentGeoPoint());//我的位置

// 定位
	private void showLocation(GeoPoint location) {
		if (null != location) {
			mapController.animateTo(location);
			mapController.setZoom(15);//缩放等级1-21
		}
	}


// 获得当前经纬度并返回GeoPoint对象
	private GeoPoint getCurrentGeoPoint() {
		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		return new GeoPoint((int) (location.getLatitude() * 1e6),
				(int) (location.getLongitude() * 1e6));
	}

 

   添加目的地(另一个经纬度坐标)

    初始化定位信息

   GeoPoint endGeoPoint = new GeoPoint(39950017,116310144);

 
private void initLocation() {
                final MyLocationOverlay overlay;
		overlay = new MyLocationOverlay(this, map);	//建地图层
                overlay.enableMyLocation(); // 监听来自位置的更新
		overlay.runOnFirstFix(new Runnable() {		//每次更新执行
			public void run() {
			mapController.setZoom(17);	//缩放
			mapController.animateTo(overlay.getMyLocation());	//指定地图显示所在位置
			}
		});
		map.getOverlays().add(overlay);	//将定位层加入坐标层中
                map.getOverlays().add(new PointOverlay(endGeoPoint));	//加入终点图标
	} 

 

 

//绘制图标,将这个类的对象加入Overlays中,自动调用draw方法

        private Projection projection;

	class PointOverlay extends Overlay {

		private GeoPoint geoPoint;

		public PointOverlay() {

		}

		public PointOverlay(GeoPoint geoPoint) {

			this.geoPoint = geoPoint;

		}

		public void draw(Canvas canvas, MapView mapv, boolean shadow) {

			super.draw(canvas, mapv, shadow);

			Point point = new Point();

			projection.toPixels(geoPoint, point);

			Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.arrow_add);

			Paint paint = new Paint();

			canvas.drawBitmap(bmp, point.x, point.y, paint);

		}

	}
 

 

   这样在布局中加两个按钮点击后调用showLocation就可以实现将地图定位到指定地点。显示定位地点图标。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值