eclipse開發android手機定位

fjssharpsword發表於2011-06-15

一、eclipse開發環境搭建

1.  JDK安裝和部署

1)  JDK下載

地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

本案例下載jdk5.0

2)  JDK安裝

預設安裝即可;

3)  JDK部署

【我的電腦】—右鍵—【屬性】—【高階】-【環境變數】:

JAVA_HOME= C:/Program Files/Java/jdk1.5.0_02,說明是jdk安裝路徑

CLASSPATH=%JAVA_HOME%/lib

   Path增加:%JAVA_HOME%/bin

2.  Eclipse安裝

地址:http://www.eclipse.org/downloads/

本案例下載eclipse3.6.2

解壓縮即可;

開啟eclipse.exe,進入【Window】—【Preferences】—【Java】檢查Jre安裝,入沒有則配置路徑。

二、android sdkadt嵌入eclispse

1ADT安裝

1)下載地址:http://www.android123.com.cn/android_kit.html

2)安裝:啟動Eclipse,選擇【Help > install new software...】,開啟介面上輸入ADT解壓地址,然後安裝即可;

本案例安裝ADT8.0.0

3.  Android sdk安裝

1)下載地址:http://developer.android.com/sdk/index.html

2)解壓即可;

3)安裝:啟動Eclipse,選擇【Window > Preferences>Android】,直接設定"SDK Location"SDK的安裝解壓目錄;

4)配置:選擇【Window>Android SDK and AVD Manager】,安裝SDK版本和

部署dvd

本案例安裝sdk2.2API8

至此eclipse開發android即可;

5android sdk升級Google API

如果要開發手機定位,則需要通過Window>Android SDK and AVD Manager】安裝Google API,本案例安裝與SDK同樣的API8Google API

三、建立android工程

1.建立mamdemo工程,包命名為cn..map,命名GMapsActivityActivity

2AndroidManifest.xml配置

<uses-library android:name="com.google.android.maps"/>--含庫,application內加

<uses-permission android:name="android.permission.INTERNET" />--網路訪問許可權,application外加

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>--GPS訪問許可權,application外加

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>--GPS訪問許可權,application外加

3.  Main.xml配置

增加:<com.google.android.maps.MapView

    android:id="@+id/MapView1"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:apiKey="0L8E3nd9sIKt0X7nSW8yOqMsAx9ftQlWotNgUXw"

/>

其中android:apiKey需要申請:

1)  debug.keystore路徑:

Window > Preferences>Android>Build】頁:default debug keystore框內路徑即是;

2)  JavaKeytool工具路徑:

%JAVA_HOME%/bin路徑下有keytool.exe

3)  產生MD5

cmd環境下,切換至debug.keystore路徑,並執行命令:keytool -list -keystore debug.keystore,當提示你輸入密碼時,輸入預設的密碼android,這樣就可以取得MD5值;

4)  獲取API key

http://code.google.com/intl/zh-CN/android/maps-api-signup.html

輸入MD5值獲取APIkey,配置到main.xml中;

5Activity開發

package cn.map;

 

import android.location.Criteria;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.provider.Settings;

import android.widget.Toast;

 

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import java.util.List;

 

import android.content.Context;

import android.content.Intent;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Point;

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapController;

import com.google.android.maps.Overlay;

 

 

public class GMapsActivity extends MapActivity {

    private MapView     mMapView;

    private MapController mMapController;

    private GeoPoint mGeoPoint;

    double latitude,longitude;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mMapView = (MapView) findViewById(R.id.MapView1);   

        mMapView.setTraffic(true);//設定為交通模式       

        //mMapView.setSatellite(true); //設定為衛星模式       

        //mMapView.setStreetView(false);//設定為街景模式    

        mMapController = mMapView.getController(); //取得MapController物件(控制MapView)

        mMapView.setEnabled(true);

        mMapView.setClickable(true);

        mMapView.setBuiltInZoomControls(true); //設定地圖支援縮放     

        mGeoPoint = new GeoPoint((int) (23* 1000000), (int) (113* 1000000)); //設定起點為廣州      

        mMapController.animateTo(mGeoPoint); //定位到指定座標      

        mMapController.setZoom(12);//設定倍數(1-21)

       

        //新增Overlay,用於顯示標註資訊

        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();

        List<Overlay> list = mMapView.getOverlays();

        list.add(myLocationOverlay);

       

        //通過GPS獲取指定座標

        openGPSSettings();

        getLocation();

    }

    protected boolean isRouteDisplayed()

    {

        return false;

    }

    class MyLocationOverlay extends Overlay

    {

        @Override

        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)

        {

            super.draw(canvas, mapView, shadow);

            Paint paint = new Paint();

            Point myScreenCoords = new Point();

            // 將經緯度轉換成實際螢幕座標

            mapView.getProjection().toPixels(mGeoPoint, myScreenCoords);

            paint.setStrokeWidth(1);

            paint.setARGB(255, 255, 0, 0);

            paint.setStyle(Paint.Style.STROKE);

            canvas.drawText("廣州歡迎你", myScreenCoords.x, myScreenCoords.y, paint);

            return true;

        }

    }

   

    private void openGPSSettings() {       

    LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { 

        Toast.makeText(this, "GPS模組正常", Toast.LENGTH_SHORT).show();

        return;      

        }       

    Toast.makeText(this, "請開啟GPS", Toast.LENGTH_SHORT).show(); 

    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);    

    startActivityForResult(intent,0); //此為設定完成後返回到獲取介面

    }

   

    private final LocationListener locationListener = new LocationListener() {  

        public void onLocationChanged(Location location) {

        // 當座標改變時觸發此函式,如果Provider傳進相同的座標,它就不會被觸發  

            // log it when the location changes  

            if (location != null) {  

              latitude = location.getLatitude();  //維度     

             longitude= location.getLongitude();   //經度 

                mGeoPoint = new GeoPoint((int)latitude, (int)longitude);

                mMapController.animateTo(mGeoPoint); //定位到指定座標              

                mMapController.setZoom(12); //設定倍數(1-21)

            }  

        }

 

       public void onProviderDisabled(String provider) {

           // TODO Auto-generated method stub

           // Providerdisable時觸發此函式,比如GPS被關閉

       }

       public void onProviderEnabled(String provider) {

           // TODO Auto-generated method stub

           //Providerenable時觸發此函式,比如GPS被開啟  

       }

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

           // TODO Auto-generated method stub

           //Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函式

       }

    };  

       

    private void getLocation(){    

    // 獲取位置管理服務       

    LocationManager locationManager;    

    String serviceName = Context.LOCATION_SERVICE; 

    locationManager = (LocationManager) this.getSystemService(serviceName); 

    // 查詢到服務資訊    

    Criteria criteria = new Criteria();     

    criteria.setAccuracy(Criteria.ACCURACY_FINE);

    // 高精度     

    criteria.setAltitudeRequired(false);   

     criteria.setBearingRequired(false);   

    criteria.setCostAllowed(true);    

    criteria.setPowerRequirement(Criteria.POWER_LOW);

    // 低功耗       

    String provider = locationManager.getBestProvider(criteria, true);

    // 獲取GPS資訊     

     Location location = locationManager.getLastKnownLocation(provider);

    // 通過GPS獲取位置    

    if (location != null) {   

        latitude = location.getLatitude();   //維度 

        longitude= location.getLongitude();  //經度

        mGeoPoint = new GeoPoint((int)latitude, (int)longitude);

            mMapController.animateTo(mGeoPoint); //定位到指定座標              

            mMapController.setZoom(12); //設定倍數(1-21)

    }

    // 設定監聽器,自動更新的最小時間為間隔N(1秒為1*1000)或最小位移變化超過N   

    locationManager.requestLocationUpdates(provider, 100*1000, 500,locationListener);

    }

}

 

四、打包android工程

Export】—【Android】或【Android Tools】—【Export unsigned/ signed Application package】,按步驟即可生產apk檔案。關鍵步驟是重新生成API key

1.生成生產的keystore

Android Tools】—【Export signed Application package】,步驟二中選擇“create new keystore”,本案例中:Location=D:/android/workspace/mapdemo.keystorePassword=mapdemo。步驟三中輸入keystore各項資訊,本案例中:alias=mymapdemoPassword=mapdemo;其他各項照填即可;最後步驟生成apk,這個apk檔案還不能釋出,需要在後面加上生產API KEY後重新發布。

2.獲取MD5API Key

1MD5cmd命令下,切換到D:/android/workspace目錄,然後執行命令:keytool –list –keystore mapdemo.keystore,輸入密碼:mapdemo,產生MD5DA:D3:46:4F:3D:D9:BD:4C:80:B5:F2:0C:03:3B:A1:16

2http://code.google.com/intl/zh-CN/android/maps-api-signup.html

輸入MD5值獲取APIkey

android:apiKey="0L8E3nd9sIKvPOTtgAttDJpMlCynuALRiOzVz4g"

修改main.xml中原debug.keystore生產的API key

3) 重新發布apk檔案

Android Tools】—【Export signed Application package】,步驟二中選擇“Use Existing keystore”,本案例中:

Location=D:/android/workspace/mapdemo.keystorePassword=mapdemo

步驟三中也是選擇“Use existing keystore“,本案例中:alias=mymapdemoPassword=mapdemo;最後步驟生產apk檔案,可釋出。

 

 

說明:模擬器上演示地點地圖通過,但手機移動GPS定位地圖未測試!android2.1手機提示解析包出錯,android2.2手機安裝成功,和開發的skd2.2版本一致!

相關文章