日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

傳遞坐標(biāo)到Google地圖(轉(zhuǎn)) - 源碼代碼下載 - CMD100 中國(guó)手機(jī)開發(fā)者聯(lián)盟

 software1 2011-02-10
在本節(jié)中,你將繼續(xù)在前一節(jié)的基礎(chǔ)上構(gòu)造。對(duì)AndroidLBS活動(dòng)的主要修改就是傳遞坐標(biāo)到Google地圖中。你將使用Google地圖來顯示用戶的當(dāng)前位置。在main.xml文件中的唯一修改指出就是為MpaView增加一個(gè)布局。在目前版本的Android SDK中,MapView被建立為一個(gè)類View??赡茉趯淼陌姹局蠱apView會(huì)相當(dāng)于這個(gè)布局。
<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)樵谶@個(gè)活動(dòng)中嵌入MapView,你需要改變類的定義?,F(xiàn)在,主要類擴(kuò)展了活動(dòng)。但是要正確的使用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置于地圖中央。這兩個(gè)包裝在使用MapView時(shí)非常的關(guān)鍵。
現(xiàn)在準(zhǔn)備增加建立地圖并傳遞坐標(biāo)的代碼。首先,設(shè)置一個(gè)一個(gè)MapView,并且從main.xml文件中把它賦值到布局:
MapView myMap = (MapView) findViewById(R.id.myMap);
下一步,設(shè)置一個(gè)Point并且把從GPS檢索的數(shù)值賦值給latPoint和IngPoint:
Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());
現(xiàn)在,可以創(chuàng)建MapController了,它將被用于移動(dòng)Google地圖來定位你定義的Point。從MapView使用getController()方法在定制的地圖中建立一個(gè)控制器:
MapController myMapController = myMap.getController();
唯一剩下的工作就是使用控制器來移動(dòng)地圖到你的位置(要讓地圖更容易辨認(rèn),把zoom設(shè)定為9):
myMapController.centerMapTo(myLocation, false);
myMapController.zoomTo(9);
你剛才所寫的所有代碼就是從活動(dòng)中利用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)行活動(dòng)?;顒?dòng)應(yīng)當(dāng)打開一個(gè)空白的地圖。點(diǎn)擊“Where Am I”按鈕,應(yīng)當(dāng)會(huì)看到地圖聚焦并且放大到舊金山??纯聪聢D就會(huì)知道地圖會(huì)如何出現(xiàn)(略)。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多