GIS的学习(十七)Osmdroid中地图图层

  在地图中添加小地图和线路图层的使用:

源代码:

package com.geoserver.osmdroid;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.MyLocationOverlay;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.SimpleLocationOverlay;
import org.osmdroid.views.overlay.MinimapOverlay;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

/**
 * 
 * 在osmdroid中 1.添加线路图层 
 * 			   2.添加小地图图层
 *             3.跟踪当前位置移动
 * 
 * 
 * @Title:
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:
 * @Date:2012-8-24
 * @author longgangbai
 * @version 1.0
 */
public class GeoServerosmdroidActivity extends Activity {
	private MapView mMapView;
	private MapController mMapController;
	int mIncr = 10000;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setTileSource(TileSourceFactory.MAPNIK);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setMultiTouchControls(true);
		mMapController = mMapView.getController();
		mMapController.setZoom(13);
		//定位当前位置
		MyLocationOverlay mylocationLayer=new MyLocationOverlay(GeoServerosmdroidActivity.this,mMapView);
		mMapView.getOverlays().add(mylocationLayer);
		mylocationLayer.enableCompass();
		mylocationLayer.enableFollowLocation();
		mylocationLayer.enableMyLocation();
		//缩放图层
		ScaleBarOverlay scalebar=new ScaleBarOverlay(GeoServerosmdroidActivity.this);
		mMapView.getOverlays().add(scalebar);
		
		//
		SimpleLocationOverlay simpleLocation=new SimpleLocationOverlay(GeoServerosmdroidActivity.this);
		mMapView.getOverlays().add(simpleLocation);
		
		//小地图
		MinimapOverlay minmap=new MinimapOverlay(GeoServerosmdroidActivity.this,mMapView.getHandler());
		mMapView.getOverlays().add(minmap);
		
		//线路每段的坐标
		GeoPoint gPt0 = new GeoPoint(51500000, -150000); 
		GeoPoint gPt1 = new GeoPoint(gPt0.getLatitudeE6() + mIncr,
				gPt0.getLongitudeE6());
		GeoPoint gPt2 = new GeoPoint(gPt0.getLatitudeE6() + mIncr,
				gPt0.getLongitudeE6() + mIncr);
		GeoPoint gPt3 = new GeoPoint(gPt0.getLatitudeE6(),
				gPt0.getLongitudeE6() + mIncr);
		mMapController.setCenter(gPt0);
		//线路
		PathOverlay myPath = new PathOverlay(Color.RED, this);
		myPath.addPoint(gPt0);
		myPath.addPoint(gPt1);
		myPath.addPoint(gPt2);
		myPath.addPoint(gPt3);
		myPath.addPoint(gPt0);
		mMapView.getOverlays().add(myPath);
	}
}

 

 

主要配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.easyway.osmdroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
    <application     android:icon="@drawable/ic_launcher"     
        android:label="@string/app_name" >     
        <activity         android:name="GeoServerosmdroidActivity"         
            android:label="@string/app_name" >         
            <intent-filter>             
                <action android:name="android.intent.action.MAIN" />              
                <category android:name="android.intent.category.LAUNCHER" />         
			</intent-filter>     
		</activity> 
	</application> 
</manifest>

 

 

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#FFFFFF"
    android:id="@+id/geoserver_layout"
    android:layout_height="fill_parent" >
    <org.osmdroid.views.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:enabled="true"
        />

</RelativeLayout>

 

你可能感兴趣的:(android,MinimapOverlay,ScaleBarOverlay,osmdroid,PathOverlay)