Android演習

2.緯度・経度の指定
(エミュレータでも可能)
Android APIキーを取得しておくこと。
【AndroidManifest.xml】
・・・
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
B.関連クラス
クラス
概
要
com.google.android.maps.GeoPointクラス
GeoPoint(int lt, int lg)
com.google.android.maps.MapViewクラス
緯度・経度から地点を作成
MapController getController()
com.google.android.maps.MapControllerクラス
マップコントローラ取得
void animateTo(GetPoint gp)
指定地点にアニメーションして動か
す
C.プログラム例(その1)
package jp.map;
import com.google.android.maps.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class SetgpActivity extends MapActivity {
MapView mapV;
EditText[] editT=new EditText[6];
TextView[] textV=new TextView[6];
Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout TL=new TableLayout(this);
setContentView(TL);
for(int i=0; i<editT.length;i++){
editT[i]=new EditText(this); textV[i]=new TextView(this);
}
プログラム例(その2)
textV[0].setText("度");
textV[1].setText("分");
textV[2].setText("秒");
textV[3].setText("度");
textV[4].setText("分");
textV[5].setText("秒");
TableRow row1=new TableRow(this);
TableRow row2=new TableRow(this);
row1.addView(editT[0]);row1.addView(textV[0]);
row1.addView(editT[1]);row1.addView(textV[1]);
row1.addView(editT[2]);row1.addView(textV[2]);
row2.addView(editT[3]);row2.addView(textV[3]);
row2.addView(editT[4]);row2.addView(textV[4]);
row2.addView(editT[5]);row2.addView(textV[5]);
button=new Button(this);
button.setText("検索");
mapV=new MapView(this,"XXXXXXXXXXXXX");
mapV.setEnabled(true);
mapV.setClickable(true);
mapV.setBuiltInZoomControls(true);
プログラム例(その3)
mapC.setZoom(3);
TL.addView(row1);
TL.addView(row2);
TL.addView(button);
TL.addView(mapV);
button.setOnClickListener(new gpClickListener());
}
protected boolean isRouteDisplayed() {
return false;
}
class gpClickListener implements OnClickListener{
public void onClick(View v){
try{
double[] d =new double[6];
for(int i=0;i<editT.length;i++){
String str=editT[i].getText().toString();
d[i]=Double.parseDouble(str);
}
プログラム例(その4)
int latitude =
// 緯度計算
(int)((d[0]+d[1]/60+d[2]/3600)*1000000);
int longitude =
// 経度計算
(int)((d[3]+d[4]/60+d[5]/3600)*1000000);
GeoPoint gp=new GeoPoint(latitude,longitude);
mapV.getController().animateTo(gp);
}catch(NumberFormatException e){
Toast.makeText(getBaseContext(),"数値を入力して下さい",
Toast.LENGTH_LONG).show();
}
}
}
}
D. 実行例
緯度(北緯)、経度(東経)を指定して「検索」ボタンを押すと移動