たぼさんの部屋

いちょぼとのんびり

M002_MapView_LocationManager

f:id:donsuka_kk:20121204171253p:plain

MyMapActivity.java

package com.efolab.mapver1_1;

import android.content.Context;
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;

public class MyMapActivity extends MapActivity implements LocationListener{
	private final static String API_KEY = "KEY";	//mac用key
	Context context;
	LinearLayout base;
	LinearLayout lHeader , lMain , lFooter;
	
	//LocationManager
	LocationManager mLocationManager;
	//MapControler
	MapController mMapController;
	
	TextView tv0 , tv1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    context = getApplicationContext();
	    base = new LinearLayout(context);
	    base.setOrientation(LinearLayout.VERTICAL);
	    setContentView(base);
	    
	    //Layout
	    lHeader = new LinearLayout(context);
	    lMain = new LinearLayout(context);
	    lFooter = new LinearLayout(context);
	    
	    //base.addView
	    base.addView(lHeader);
	    base.addView(lMain , new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , 0 , 1.0f));
	    base.addView(lFooter);
	    
	    //map
	    MapView mapView = new MapView(this , API_KEY);	//XXX thisをわたす *context ではエラー発生!!!
	    mapView.setEnabled(true);
	    mapView.setClickable(true);
	    
	    //control
	    tv0 = new TextView(context);
	    tv0.setText("HEADER");
	    tv1 = new TextView(context);
	    tv1.setText("FOOTER");
	    
	    //contents.addView
	    lHeader.addView(tv0);
	    lMain.addView(mapView);
	    lFooter.addView(tv1);
	    
	    //MapController
	    mMapController = mapView.getController();
	    mapView.getController().setZoom(20);
	    mapView.setBuiltInZoomControls(true);
	    mapView.invalidate();
	    
	    //LocationManager
	    mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	}
	
	
	@Override
	protected void onResume() {
		//位置情報更新の設定:更新時間:60秒、更新距離10m
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1000 , 1 , this);
		super.onResume();
	}


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLocationChanged(Location location) {
		//位置情報が更新された場合
		GeoPoint gp = new GeoPoint((int)(location.getLatitude()*1E6) , (int)(location.getLongitude()*1E6));
		//緯度の取得
		double lat = gp.getLatitudeE6() / 1E6;
		//経度の取得
		double lng = gp.getLongitudeE6() /1E6;
		//地図の中心位置を移動する
		mMapController.animateTo(gp);
		//tv set
		tv1.setText("緯度="+lat+",経度="+lng);
	}

	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	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.mapver1_1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GPSを使用する -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!-- エミュレータ用 -->
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".MyMapActivity" >
            <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>