在建立專案時,BuildSDK要選GoogleAPIs版本的
開啟網路權限以及地圖之程式庫
<uses-permission android:name="android.permission.INTERNET"/>
<uses-library android:name="com.google.android.maps"/>
<XML>
<com.google.android.maps.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:apiKey="0lDmUngY4dnrsMtVjU12ySKKSMU-y2G0edVeXXX"
<--讓地圖可以滑動-->
android:clickable="true" >
</com.google.android.maps.MapView>
<JAVA Code>
package com.example.maptest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
//必須繼承MapActivity並實作指定方法isRouteDisplayed()
public class MainActivity extends MapActivity {
//MapView變數
MapView map;
Spinner spi1,spi2;
String[]name={"台灣大學","清華大學","交通大學","成功大學"};
String[][]loc={{"25.019943","121.542353"},{"24.795621","120.998153"},{"24.791704","121.003341"}
,{"23.000875","120.218017"}};
String[]type={"街道圖","衛星圖"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spi1=(Spinner)findViewById(R.id.spinner1);
spi2=(Spinner)findViewById(R.id.spinner2);
setAdapter();
map=(MapView)findViewById(R.id.map);
//使用MapView變數.getController(),取得MapController物件
final MapController con=map.getController();
//設定地圖縮放比例
con.setZoom(18);
//加上使用者自行地圖縮放功能
map.setBuiltInZoomControls(true);
spi1.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1,
int location, long arg3) {
// TODO Auto-generated method stub
switch(location){
case 0:
startMap(0);
break;
case 1:
startMap(1);
break;
case 2:
startMap(2);
break;
case 3:
startMap(3);
break;
}
}
private void startMap(int i) {
// TODO Auto-generated method stub
//設定地理位置經緯度,Double型別
double x=Double.parseDouble(loc[i][0]);
double y=Double.parseDouble(loc[i][1]);
//宣告GeoPoint變數物件,建構參數必須傳入經緯度,
//並將地理位置座標乘上100,0000或是1e6,並轉回int型別
GeoPoint geo=new GeoPoint((int)(x*1e6), (int)(y*1e6));
//將GeoPoint傳給MapController,並顯示。
con.animateTo(geo);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spi2.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1,
int location, long arg3) {
// TODO Auto-generated method stub
switch(location){
case 0:
//MapView物件變數可使用setSatellite(boolean)來呈現衛星地圖
map.setSatellite(false);
break;
case 1:
map.setSatellite(true);
break;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@SuppressLint("NewApi")
private void setAdapter() {
// TODO Auto-generated method stub
ArrayAdapter ada1=new ArrayAdapter(this, android.R.layout.simple_spinner_item);
ada1.addAll(name);
spi1.setAdapter(ada1);
ArrayAdapter ada2=new ArrayAdapter(this, android.R.layout.simple_spinner_item);
ada2.addAll(type);
spi2.setAdapter(ada2);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
沒有留言:
張貼留言