2013年3月21日 星期四

Google Map Location

Android Location API - Tutorial

Android - 定位資訊(GPS)相關類別使用說明

Android 開發筆記 - 透過 Google Maps API 畫出 GPS 路徑

[Android] Google地圖標誌


Android - 定位資訊(GPS)相關類別使用說明

Stop Location Listener in Android


看了那麼多
我們來練習一下
GoogleMapTrackTest.class

package com.example.googlemaptracktest;

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

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ZoomControls;

public class GoogleMapTrackTest extends MapActivity {
    private MapController mapController = null;
    private MapView mMapview = null;
    private ZoomControls myZoom = null;
    private Drawable startdrawable = null;
    private Drawable enddrawable = null;
    private MapItemizedOverlay mymapoverlay = null;
    private boolean firstLocation = true;
    GeoPoint startPoint = null;

    private LocationManager mLocationManager = null;
    private String strLocationPrivider = "";
    private Location mLocation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

        LinearLayout maplinearlayout;

        maplinearlayout = (LinearLayout) findViewById(R.id.zoomview);
        mMapview = (MapView) findViewById(R.id.mapview);
        myZoom = (ZoomControls) (mMapview).getZoomControls();
        mapController = mMapview.getController();
        maplinearlayout.addView(myZoom);

        startdrawable = this.getResources().getDrawable(R.drawable.b1);// 指定標誌圖示
        enddrawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        mymapoverlay = new MapItemizedOverlay(startdrawable, mMapview);

        // Location
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                || mLocationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            Criteria mCriteria01 = new Criteria();
            mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
            mCriteria01.setAltitudeRequired(false);
            mCriteria01.setBearingRequired(false);
            mCriteria01.setCostAllowed(true);
            mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
            strLocationPrivider = mLocationManager.getBestProvider(mCriteria01, true);
            mLocation = mLocationManager
                    .getLastKnownLocation(strLocationPrivider);

            // Initialize the location fields
            if (mLocation != null) {
                int lat = (int) (mLocation.getLatitude());
                int lng = (int) (mLocation.getLongitude());
                Toast.makeText(GoogleMapTrackTest.this,
                        String.valueOf(lat) + "," + String.valueOf(lng),
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(GoogleMapTrackTest.this,
                        "Location not available", Toast.LENGTH_SHORT).show();
                mMapview.getController().setCenter(new GeoPoint(24778289, 120988108));
                mapController.setZoom(14);
            }
        }

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location
                // provider.
                int lat = (int) (location.getLatitude() * 1e6);
                int lng = (int) (location.getLongitude() * 1e6);
                GeoPoint addPoint = new GeoPoint(lat, lng); // 指定位置

                if (firstLocation == true) {
                    Toast.makeText(GoogleMapTrackTest.this,
                            String.valueOf(lat) + "," + String.valueOf(lng),
                            Toast.LENGTH_SHORT).show();
                    firstLocation = false;
                    startPoint = addPoint;
                    // 加入經緯度
                    OverlayItem myoverlayitem = new OverlayItem(addPoint, null,    null);
                    mymapoverlay.addOverlayItem(myoverlayitem);
                    mMapview.getOverlays().add(mymapoverlay);
                    // 依照mypoint設定之經緯度為中心點

                    mymapoverlay = new MapItemizedOverlay(enddrawable, mMapview);
                    // 換圖示
                } else {
                    Toast.makeText(GoogleMapTrackTest.this,
                            String.valueOf(lat) + "," + String.valueOf(lng),
                            Toast.LENGTH_SHORT).show();
                    OverlayItem myoverlayitem = new OverlayItem(addPoint, null,    null);
                    mymapoverlay.addOverlayItem(myoverlayitem, startPoint);
                    mMapview.getOverlays().add(mymapoverlay);
                }
                mMapview.getController().setCenter(addPoint);
                mapController.setZoom(14);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
                
            }

            public void onProviderEnabled(String provider) {
                Toast.makeText(GoogleMapTrackTest.this, " onProviderEnabled ",
                        Toast.LENGTH_SHORT).show();
            }

            public void onProviderDisabled(String provider) {
                Toast.makeText(GoogleMapTrackTest.this, " onProviderDisabled ",
                        Toast.LENGTH_SHORT).show();
            }

        };

        // Register the listener with the Location Manager to receive location
        // updates
        mLocationManager.requestLocationUpdates(strLocationPrivider, 0, 0,
                locationListener);
    }

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

}



MapItemizedOverlay.class

package com.example.googlemaptracktest;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> gList = new ArrayList<OverlayItem>();
    private ArrayList<GeoPoint> gListGeoPoint = new ArrayList<GeoPoint>();
    private Drawable marker = null;
    private Context context = null;
    private boolean firstmove = true;    
    private MapView myMapView = null;

    // Projection myProjection = null;
    // Point mapViewOut = null;

    public MapItemizedOverlay(Drawable arg0, MapView mMapview) {
        super(arg0);
        // TODO Auto-generated constructor stub
        marker = arg0;
        context = mMapview.getContext();
        myMapView = mMapview;
    }

    public void addOverlayItem(OverlayItem oItem) {
        gList.clear();
        gList.add(oItem);
        populate();
    }

    public void addOverlayItem(OverlayItem oItem, GeoPoint startPoint) {
        if (firstmove == true) {
            gListGeoPoint.add(startPoint);
            firstmove = false;
        }
        gListGeoPoint.add(oItem.getPoint());
        gList.clear();
        gList.add(oItem);
        populate();
    }

    @Override
    protected OverlayItem createItem(int arg0) {
        // TODO Auto-generated method stub
        return gList.get(arg0);
    }

    public void draw(Canvas canvas, com.google.android.maps.MapView mapview,
            boolean shadow) {
        super.draw(canvas, mapview, shadow);
        boundCenterBottom(marker);
        
         Projection myProjection = myMapView.getProjection();
         Point out;
         Path myPath = new Path(); 
         
        for (int i = 0; i < gListGeoPoint.size(); i++) {
            GeoPoint in = new GeoPoint(gListGeoPoint.get(i).getLatitudeE6(),
                    gListGeoPoint.get(i).getLongitudeE6());
            out = new Point();
            myProjection.toPixels(in, out);
            if (i == 0) {
                myPath.moveTo(out.x, out.y);
            } else {
                myPath.lineTo(out.x, out.y);
                myPath.setLastPoint(out.x, out.y);
            }
        }
         
         Paint myPaint = new Paint();
         myPaint.setColor(Color.RED);
         myPaint.setStyle(Paint.Style.STROKE);
         myPaint.setStrokeWidth(2);
         myPaint.setAlpha(70);
        
         canvas.drawPath(myPath, myPaint);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return gList.size();
    }

}



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

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

    <permission
        android:name="com.example.googlemaptracktest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />
        <activity
            android:name="com.example.googlemaptracktest.GoogleMapTrackTest"
            android:label="@string/app_name"  android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/zoomview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="04PWltFqrvM9KGKh8Dvlh3hN73M_xFBck0zmI5w"
        android:clickable="true"
        android:enabled="true" 
        />

</LinearLayout>


Result:

























我拿了我的實機測試
GPS抓得很慢!!!!!!
所以要等一下
僅供參考喽!









沒有留言:

張貼留言