在本節(jié)中,你將繼續(xù)在前一節(jié)的基礎(chǔ)上構(gòu)造。對AndroidLBS活動的主要修改就是傳遞坐標(biāo)到Google地圖中。你將使用Google地圖來顯示用戶的當(dāng)前位置。在main.xml文件中的唯一修改指出就是為MpaView增加一個布局。在目前版本的Android SDK中,MapView被建立為一個類View??赡茉趯淼陌姹局蠱apView會相當(dāng)于這個布局。
<view class="com.google.android.maps.MapView"
android:id="@+id/myMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> |
完成后的main.xml文件應(yīng)當(dāng)像這樣:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas./apk/res/android
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/gpsButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Where Am I"
/>
<LinearLayout xmlns:android=http://schemas./apk/res/android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/latLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude: "
/>
<TextView
android:id="@+id/latText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android=http://schemas./apk/res/android
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lngLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude: "
/>
<TextView
android:id="@+id/lngText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<view class="com.google.android.maps.MapView"
android:id="@+id/myMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout> |
因?yàn)樵谶@個活動中嵌入MapView,你需要改變類的定義?,F(xiàn)在,主要類擴(kuò)展了活動。但是要正確的使用Google MapView,你必須擴(kuò)展MapActivity。因此,你需要輸入MapActivity包裝并且替換在頭部的Activity 包裝。
輸入下列包裝:
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.android.maps.MapController |
Point包裝將被用于保留point的值,它就是展示地圖坐標(biāo)的,而MapController將你的point置于地圖中央。這兩個包裝在使用MapView時非常的關(guān)鍵。
現(xiàn)在準(zhǔn)備增加建立地圖并傳遞坐標(biāo)的代碼。首先,設(shè)置一個一個MapView,并且從main.xml文件中把它賦值到布局:
MapView myMap = (MapView) findViewById(R.id.myMap); |
下一步,設(shè)置一個Point并且把從GPS檢索的數(shù)值賦值給latPoint和IngPoint:
Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue()); |
現(xiàn)在,可以創(chuàng)建MapController了,它將被用于移動Google地圖來定位你定義的Point。從MapView使用getController()方法在定制的地圖中建立一個控制器:
MapController myMapController = myMap.getController(); |
唯一剩下的工作就是使用控制器來移動地圖到你的位置(要讓地圖更容易辨認(rèn),把zoom設(shè)定為9):
myMapController.centerMapTo(myLocation, false);
myMapController.zoomTo(9); |
你剛才所寫的所有代碼就是從活動中利用Google地圖。完整的類應(yīng)當(dāng)像這樣:
package android_programmers_guide.AndroidLBS;
import android.os.Bundle;
import android.location.LocationManager;
import android.view.View;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Point;
import com.google.android.maps.MapController;
public class AndroidLBS extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final Button gpsButton = (Button) findViewById(R.id.gpsButton);
gpsButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
LoadProviders();
}});
}
public void LoadProviders(){
TextView latText = (TextView) findViewById(R.id.latText);
TextView lngText = (TextView) findViewById(R.id.lngText);
LocationManager myManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Double latPoint =
myManager.getCurrentLocation("gps").getLatitude()*1E6;
Double lngPoint =
myManager.getCurrentLocation("gps").getLongitude()*1E6;
latText.setText(latPoint.toString());
lngText.setText(lngPoint.toString());
MapView myMap = (MapView) findViewById(R.id.myMap);
Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());
MapController myMapController = myMap.getController();
myMapController.centerMapTo(myLocation, false);
myMapController.zoomTo(9);
}
} |
在模擬器中運(yùn)行活動?;顒討?yīng)當(dāng)打開一個空白的地圖。點(diǎn)擊“Where Am I”按鈕,應(yīng)當(dāng)會看到地圖聚焦并且放大到舊金山??纯聪聢D就會知道地圖會如何出現(xiàn)(略)。
|