たぼさんの部屋

いちょぼとのんびり

SECTION097 地図画面上に別の画像を重ねて表示する

f:id:donsuka_kk:20121211105220p:plain

Main.java

package com.efolab.s097;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class Main extends MapActivity implements LocationListener{
	Context context;
	//LocationManagerの宣言
	LocationManager mLocationManager;
	//MapControllerの宣言
	MapController mMapController;
	//text
	TextView tv;
	//LinearLayout
	LinearLayout base;
	//MAP key
	private final String MAP_KEY = "0HrEFkF4qrKk7s-4S8RbWvKimK2sFJHUKRAkQJQ";	//mac
	MapView mapview;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    context = getApplicationContext();
	    base = new LinearLayout(context);
	    base.setOrientation(LinearLayout.VERTICAL);
	    setContentView(base);
	    
	    //MapViewの利用
	    mapview = new MapView(this , MAP_KEY);
	    //MapControllerの取得
	    mMapController = mapview.getController();
	    //ズームコントローラを配置する
	    mapview.getController().setZoom(16);
	    mapview.setBuiltInZoomControls(true);
	    mapview.invalidate();
	    //ロケーションマネージャの取得
	    mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
	    //位置情報更新の設定
	    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000 , 10 , this);
	    //テキストビュー
	    tv = new TextView(context);
	    tv.setText("現在位置を取得しています");
	    
	    //base AddView
	    base.addView(tv);
	    base.addView(mapview , new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT , 0 , 1.0f));
	}
	//isRouteDisplayedメソッドを実装する
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	//位置情報が更新された場合
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		double lat = location.getLatitude();
		double lng = location.getLongitude();
		GeoPoint gp = new GeoPoint((int)(lat * 1e6) , (int)(lng * 1e6));
		//地図の中心位置を移動する
		mMapController.animateTo(gp);
		//取得した位置情報をテキストビューへ出力する
		tv.setText("緯度="+lat+",経度="+lng);
		
		//オーバーレイに表示する画像の設定
		Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		AddOverlay overlay = new AddOverlay(icon , gp);
		//マップビューにオーバーレイを追加する
		List<Overlay> overlays = mapview.getOverlays();
		overlays.add(overlay);
	}
	//地図上に表示されるオーバーレイ
	private class AddOverlay extends Overlay{
		//描画する画像
		Bitmap mImage;
		int mOffsetX;
		int mOffsetY;
		//画像を表示する位置
		GeoPoint mPoint;
		AddOverlay(Bitmap image , GeoPoint initial){
			mImage = image;
			mOffsetX = 0 - image.getWidth() / 2;
			mOffsetY = 0 - image.getHeight() / 2;
			mPoint = initial;
		}
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			// TODO Auto-generated method stub
			super.draw(canvas, mapView, shadow);
			//画面上の位置と地図上の位置の相互変換を行う
			Projection projection = mapView.getProjection();
			Point point = new Point();
			projection.toPixels(mPoint , point);
			point.offset(mOffsetX, mOffsetY);
			//オーバーレイへ描画する
			canvas.drawBitmap(mImage, point.x, point.y,null);
		}
		
	}
	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.efolab.s097"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
	<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="Main">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps"/>
    </application>

</manifest>