逐风林羽 发表于 2013-1-14 17:59:15

[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就可以实现将地图定位到指定地点。显示定位地点图标。
页: [1]
查看完整版本: [Android]googleMap的简单使用,地图定位,图标绘制。