一、eclipse開發(fā)環(huán)境搭建
1. JDK安裝和部署
1) JDK下載
地址:http://www.Oracle.com/technetwork/java/javase/downloads/index.html
本案例下載jdk5.0
2) JDK安裝
默認(rèn)安裝即可;
3) JDK部署
【我的電腦】—右鍵—【屬性】—【高級(jí)】-【環(huán)境變量】:
JAVA_HOME= C:/Program Files/Java/jdk1.5.0_02,說明是jdk安裝路徑
CLASSPATH=%JAVA_HOME%/lib
Path增加:%JAVA_HOME%/bin
2. Eclipse安裝
本案例下載eclipse3.6.2
解壓縮即可;
打開eclipse.exe,進(jìn)入【Window】—【Preferences】—【Java】檢查Jre安裝,入沒有則配置路徑。
二、Android sdk和adt嵌入eclispse
1.ADT安裝
1)下載地址:http://www./android_kit.html
2)安裝:?jiǎn)?dòng)Eclipse,選擇【Help】 > 【install new software...】,打開界面上輸入ADT解壓地址,然后安裝即可;
本案例安裝ADT8.0.0
3. Android sdk安裝
1)下載地址:http://developer./sdk/index.html
2)解壓即可;
3)安裝:?jiǎn)?dòng)Eclipse,選擇【Window】 > 【Preferences】>【Android】,直接設(shè)定"SDK Location"為SDK的安裝解壓目錄;
4)配置:選擇【Window】>【Android SDK and AVD Manager】,安裝SDK版本和
部署dvd;
本案例安裝sdk2.2,API8
至此eclipse開發(fā)android即可;
5)android sdk升級(jí)Google API
如果要開發(fā)手機(jī)定位,則需要通過【Window】>【Android SDK and AVD Manager】安裝Google API,本案例安裝與SDK同樣的API8的Google API;
三、創(chuàng)建android工程
1.創(chuàng)建mamdemo工程,包命名為cn..map,命名GMapsActivity的Activity;
2.AndroidManifest.xml配置
<uses-library android:name="com.google.android.maps"/>--含庫,application內(nèi)加
<uses-permission android:name="android.permission.INTERNET" />--網(wǎng)絡(luò)訪問權(quán)限,application外加
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>--GPS訪問權(quán)限,application外加
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>--GPS訪問權(quán)限,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需要申請(qǐng):
1) debug.keystore路徑:
【Window】 > 【Preferences】>【Android】>【Build】頁:default debug keystore框內(nèi)路徑即是;
2) Java的Keytool工具路徑:
%JAVA_HOME%/bin路徑下有keytool.exe;
3) 產(chǎn)生MD5
在cmd環(huán)境下,切換至debug.keystore路徑,并執(zhí)行命令:keytool -list -keystore debug.keystore,當(dāng)提示你輸入密碼時(shí),輸入默認(rèn)的密碼android,這樣就可以取得MD5值;
4) 獲取API key
http://code.google.com/intl/zh-CN/android/maps-api-signup.html
輸入MD5值獲取APIkey,配置到main.xml中;
5.Activity開發(fā)
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);//設(shè)置為交通模式
//mMapView.setSatellite(true); //設(shè)置為衛(wèi)星模式
//mMapView.setStreetView(false);//設(shè)置為街景模式
mMapController = mMapView.getController(); //取得MapController對(duì)象(控制MapView)
mMapView.setEnabled(true);
mMapView.setClickable(true);
mMapView.setBuiltInZoomControls(true); //設(shè)置地圖支持縮放
mGeoPoint = new GeoPoint((int) (23* 1000000), (int) (113* 1000000)); //設(shè)置起點(diǎn)為廣州
mMapController.animateTo(mGeoPoint); //定位到指定坐標(biāo)
mMapController.setZoom(12);//設(shè)置倍數(shù)(1-21)
//添加Overlay,用于顯示標(biāo)注信息
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = mMapView.getOverlays();
list.add(myLocationOverlay);
//通過GPS獲取指定坐標(biāo)
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();
// 將經(jīng)緯度轉(zhuǎn)換成實(shí)際屏幕坐標(biāo)
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, "請(qǐng)開啟GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此為設(shè)置完成后返回到獲取界面
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// 當(dāng)坐標(biāo)改變時(shí)觸發(fā)此函數(shù),如果Provider傳進(jìn)相同的坐標(biāo),它就不會(huì)被觸發(fā)
// log it when the location changes
if (location != null) {
latitude = location.getLatitude(); //維度
longitude= location.getLongitude(); //經(jīng)度
mGeoPoint = new GeoPoint((int)latitude, (int)longitude);
mMapController.animateTo(mGeoPoint); //定位到指定坐標(biāo)
mMapController.setZoom(12); //設(shè)置倍數(shù)(1-21)
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
// Provider被disable時(shí)觸發(fā)此函數(shù),比如GPS被關(guān)閉
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
//Provider被enable時(shí)觸發(fā)此函數(shù),比如GPS被打開
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
//Provider的轉(zhuǎn)態(tài)在可用、暫時(shí)不可用和無服務(wù)三個(gè)狀態(tài)直接切換時(shí)觸發(fā)此函數(shù)
}
};
private void getLocation(){
// 獲取位置管理服務(wù)
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服務(wù)信息
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(); //經(jīng)度
mGeoPoint = new GeoPoint((int)latitude, (int)longitude);
mMapController.animateTo(mGeoPoint); //定位到指定坐標(biāo)
mMapController.setZoom(12); //設(shè)置倍數(shù)(1-21)
}
// 設(shè)置監(jiān)聽器,自動(dòng)更新的最小時(shí)間為間隔N秒(1秒為1*1000)或最小位移變化超過N米
locationManager.requestLocationUpdates(provider, 100*1000, 500,locationListener);
}
}
四、打包android工程
【Export】—【Android】或【Android Tools】—【Export unsigned/ signed Application package】,按步驟即可生產(chǎn)apk文件。關(guān)鍵步驟是重新生成API key:
1.生成生產(chǎn)的keystore:
【Android Tools】—【Export signed Application package】,步驟二中選擇“create new keystore”,本案例中:Location=D:/android/workspace/mapdemo.keystore;Password=mapdemo。步驟三中輸入keystore各項(xiàng)信息,本案例中:alias=mymapdemo;Password=mapdemo;其他各項(xiàng)照填即可;最后步驟生成apk,這個(gè)apk文件還不能發(fā)布,需要在后面加上生產(chǎn)API KEY后重新發(fā)布。
2.獲取MD5和API Key
1)MD5:cmd命令下,切換到D:/android/workspace目錄,然后執(zhí)行命令:keytool –list –keystore mapdemo.keystore,輸入密碼:mapdemo,產(chǎn)生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生產(chǎn)的API key;
3) 重新發(fā)布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;最后步驟生產(chǎn)apk文件,可發(fā)布。
說明:模擬器上演示地點(diǎn)地圖通過,但手機(jī)移動(dòng)GPS定位地圖未測(cè)試!android2.1手機(jī)提示解析包出錯(cuò),android2.2手機(jī)安裝成功,和開發(fā)的skd2.2版本一致!