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

[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 楼 luwies 2012-09-07  
请教一下,我用的是Google APIs[Android 4.1],按照您的文章写了一下这个工程。
<com.google.android.maps.MapView 
    android:id="@+id/map"   
    android:apiKey="**********************************"       
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"    
    android:clickable="true" /> 
apikey的内容我已经替换成我自己的了。
在这个代码中有这样的一个错误,--->>>error: No resource identifier found for attribute 'apikey' in package 'android'
我的AndroidManifest.xml如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <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"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps"
            android:required="true"/>
        <activity
            android:name=".GoogleMapActivity"
            android:label="@string/title_activity_google_map" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>   
</manifest>
[Android]googleMap的简单使用,map定位,图标绘制 [Android]googleMap的简单使用,map定位,图标绘制 希望您能帮我解决一下这个问题。。。O(∩_∩)O谢谢
2 楼 逐风林羽 2012-09-07  
luwies 写道
请教一下,我用的是Google APIs[Android 4.1],按照您的文章写了一下这个工程。
<com.google.android.maps.MapView 
    android:id="@+id/map"   
    android:apiKey="**********************************"       
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"    
    android:clickable="true" /> 
apikey的内容我已经替换成我自己的了。
在这个代码中有这样的一个错误,--->>>error: No resource identifier found for attribute 'apikey' in package 'android'
我的AndroidManifest.xml如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <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"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps"
            android:required="true"/>
        <activity
            android:name=".GoogleMapActivity"
            android:label="@string/title_activity_google_map" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>   
</manifest>
[Android]googleMap的简单使用,map定位,图标绘制 [Android]googleMap的简单使用,map定位,图标绘制 希望您能帮我解决一下这个问题。。。O(∩_∩)O谢谢

有点需要看一下 注意android:apiKey的拼写,要注意大小写。我用的是GoogleAPIs 2.2 你也换下api试一下。如果是key值不对 不会报出异常,只是无法显示地图层。你再看一下。
3 楼 luwies 2012-09-07  
O(∩_∩)O谢谢:oops: 真是大写的,我太不小心了,这个apiKey居然是不能用Alt+/调出来的。。。