歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Eclipse開發Android手機定位

Eclipse開發Android手機定位

日期:2017/3/1 11:16:52   编辑:Linux編程

一、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 sdk和adt嵌入eclispse

1.ADT安裝

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.2,API8

至此eclipse開發android即可;

5)android sdk升級Google API

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

三、創建android工程

1.創建mamdemo工程,包命名為cn..map,命名GMapsActivity的Activity;

2.AndroidManifest.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) Java的Keytool工具路徑:

%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中;

5.Activity開發

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

// Provider被disable時觸發此函數,比如GPS被關閉

}

public void onProviderEnabled(String provider) {

// TODO Auto-generated method stub

//Provider被enable時觸發此函數,比如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.keystore;Password=mapdemo。步驟三中輸入keystore各項信息,本案例中:alias=mymapdemo;Password=mapdemo;其他各項照填即可;最後步驟生成apk,這個apk文件還不能發布,需要在後面加上生產API KEY後重新發布。

2.獲取MD5和API Key

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

2)http://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.keystore;Password=mapdemo;

步驟三中也是選擇“Use existing keystore“,本案例中:alias=mymapdemo;Password=mapdemo;最後步驟生產apk文件,可發布。

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

Copyright © Linux教程網 All Rights Reserved