1 / 19

8-4  Google マップの利用 1.地図を表示 ( エミュレータでも可能 )

8-4  Google マップの利用 1.地図を表示 ( エミュレータでも可能 ). Android API キーを取得しておくこと。 【AndroidManifest.xml】  ・・・ </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>. B .関連クラス.

leann
Download Presentation

8-4  Google マップの利用 1.地図を表示 ( エミュレータでも可能 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8-4 Googleマップの利用1.地図を表示(エミュレータでも可能)8-4 Googleマップの利用1.地図を表示(エミュレータでも可能) Android APIキーを取得しておくこと。 【AndroidManifest.xml】  ・・・ </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>

  2. B.関連クラス クラス     概   要 com.google.android.maps.MapActivityクラス protected boolean isRoutDisplay() ルートが表示されるとき呼び出される。 com.google.android.maps.MapViewクラス MapView(Context context, String key) マップビュー作成(APIキー指定) voidsetClickable(boolean enable) クリック可能にする。 voidsetBuiltInZoomControls(boolean enable) ズームコントロール表示設定。

  3. C.プログラム例(その1) package jp.sample; import com.google.android.maps.*; import android.os.*; import android.widget.LinearLayout; public class SampleMapActivity extends MapActivity { MapView mapV; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout LL = new LinearLayout(this); LL.setOrientation(LinearLayout.VERTICAL); setContentView(LL); mapV=new MapView(this, "XXXXXXXXXXXXX"); mapV.setEnabled(true); mapV.setClickable(true); mapV.setBuiltInZoomControls(true); LL.addView(mapV); }

  4. プログラム例(その2) protected boolean isRouteDisplayed(){ return false; } }

  5. D. 実行例 ①画面をタッチすると拡大・縮小のボタンが表示されるので、これらのボタンを押すことで拡大縮小ができる。 ②表示場所の移動は、タッチして動かす。 画面をタッチすると

  6. 2.緯度・経度の指定(エミュレータでも可能)2.緯度・経度の指定(エミュレータでも可能) Android APIキーを取得しておくこと。 【AndroidManifest.xml】  ・・・ </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>

  7. 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) 指定地点にアニメーションして動かす

  8. 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); }

  9. プログラム例(その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);

  10. プログラム例(その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); }

  11. プログラム例(その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(); } } } }

  12. D. 実行例 緯度(北緯)、経度(東経)を指定して「検索」ボタンを押すと移動

  13. 3.位置情報を取得する(エミュレータでは位置情報をDDMSで与える)3.位置情報を取得する(エミュレータでは位置情報をDDMSで与える) Android APIキーを取得しておくこと。 【AndroidManifest.xml】  ・・・ </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name= "android.permission.ACCESS_FINE_LOCATION" /> </manifest>>

  14. B.関連クラス クラス     概   要 android.location.Locationクラス double getLatitude() 緯度を取得 double getLongitude() 経度を取得 android.location.LocationManagerクラス requestLocationUpdates(String provider, プロバイダ、最短更新時間、最短更新距離・ long mint, float mind,LocationListener I) リスなを登録 android.location.LocationListenerクラス void onLocationChanged(Location location) 位置情報が変更されたとき呼び出される void onProviderDisabled(String provider) プロバイダが無効になったとき呼び出される void onProviderEnabled(String provider) プロバイダが有効になったとき呼び出される void onStatusChanged(String provider, プロバイダの状態が変化したとき int status, Bundle ex) 呼び出される com.google.android.maps.MapViewクラス MapController getController() マップコントローラ取得 com.google.android.maps.MapControllerクラス void animateTo(GetPoint gp) 指定地点にアニメーションして動かす

  15. C.プログラム例(その1) package jp.getgp; import com.google.android.maps.*; import android.content.*; import android.location.*; import android.os.*; import android.widget.*; public class GetgpActivity extends MapActivity { MapView mapV; LocationManager locM; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout LL=new LinearLayout(this); LL.setOrientation(LinearLayout.VERTICAL); setContentView(LL); mapV=new MapView(this,"XXXXXXXXXXXXX"); mapV.setEnabled(true); mapV.setBuiltInZoomControls(true); MapController mapC=mapV.getController(); mapC.setZoom(12);

  16. プログラム例(その2) locM=(LocationManager) getSystemService(Context.LOCATION_SERVICE); LL.addView(mapV); } public void onResume(){ super.onResume(); locM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 50, new getgpLocationListener()); } @Override protected boolean isRouteDisplayed() { return false; } class getgpLocationListener implements LocationListener{ public void onLocationChanged(Location arg0) { // TODO 自動生成されたメソッド・スタブ int latitude = (int)(arg0.getLatitude() *1000000); int longitude = (int)(arg0.getLongitude()*1000000); GeoPoint geoP=new GeoPoint(latitude,longitude); mapV.getController().animateTo(geoP); } public void onProviderDisabled(String arg0) { // TODO 自動生成されたメソッド・スタブ } public void onProviderEnabled(String arg0) { // TODO 自動生成されたメソッド・スタブ } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO 自動生成されたメソッド・スタブ } } }

  17. プログラム例(その3) public void onProviderDisabled(String arg0) { // TODO 自動生成されたメソッド・スタブ } public void onProviderEnabled(String arg0) { // TODO 自動生成されたメソッド・スタブ } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO 自動生成されたメソッド・スタブ } } }

  18. D. 実行例(その1) ①エミュレータでは、メニューの「ウィンドウ(Window)」、「パースペクティブを開く(Open Perspective)」、「その他(Others)」を選択し、「パースペクティブを開く(Open Perspective)」ダイアログから、「DDMS(Dalvic Debug Monitor Server)」を選択し、「OK」ボタンをクリック。

  19. 実行例(その2) ②「Emulator Control」の「location Controls」の「マニュアル(Manual)」タブで「Longitude」に経度を「Latitude」に緯度を入力して「送信」ボタンをクリックすると、エミュレータの地図が変わっている。

More Related