ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 アプリケーションでグーグルマップを使います

Google Mapsにマーカを加える

Adding Markers to the Map

マップを特定の位置に誘導する
Google Mapsの表示位置を特定の場所に誘導させる方法を示しました。
マップを上下左右に動かしていて、希望の位置をセンターに表示させます。
さらにユーザが視覚的に見やすいように希望の位置に
マーカ(プッシュピンとして知られています)を立てます。
そのマーカがあればユーザはすぐに明確に位置を
把握できるようになります。
このマーカを立てる方法はいろいろあり、その方法を示します。

方法その1 Dispkaying a Marker at the Current Location

大抵、開発者から要求されるものの1つに、
LocationManagerクラスを使うこと無しにユーザの現在位置を
表示する能力です。
これについてはGPS、Wi-Fiなどを使って地理情報を取得などで
紹介もします。
ユーザの現在位置を取得するには、MyLlocationOverlayクラスも使えます。
動作させるのに必要なのはMyLlocationOverlayクラスのインスタンスを
作り、マップのオーバレイリストにそのインスタンスを加えます。


ちょっと注釈

MapViewにあるオーバレイはマップの上部に加えられる層で、
オーバレイはビットマップ、ドローイングなどを含みます。


以下のコードはMyLocationOverlayクラスを使って
マップ上に現在位置を表示する方法を示します。

package net.learn2develop.maps;

import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class MainActivity extends MapActivity{
  MapView mapView;
  MapController mc;

  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savesInstanceState);
  &emdsp;setContentView(R.layout.activity_main);

   mapView = (MapView) findViewById(R.id.mapView);
   mapView.setBuiltInZoomControls(true);

   mc = mapView.getController();

   //---get your current location and display a blue dot---
   MyLocationOverlay myLocationOverlay =
     new MyLocationOverlay(this, mapView);

   mapView.getOverlays().add(myLocationOverlay);
   myLocationOverlay.enableMyLocation();
  }

  @Override
  protected boolean isRouteDisplayed(){
   return false;
  }
}

これで現在位置が青いドットでマップ上に表現されます。


ちょっと注釈

MyLocationOverlayクラスに現在位置を見つけさせるためには、
android.permission.ACCESS_COARSE_LOCATIONか、
androidpermission.ACCESS_FINE_LOCATIONパーミッションを
加えなければなりません。

方法その2 Diaplaying a Marker at Specific Locations

現在位置にマーカを表示させます。
指定した位置にマーカを表示させたい場合には、
ItemizedOverlayクラスを使います。
ItemizedOverlayクラスはOverlayItem(マーカ)オブジェクトの
リストから出来ている抽象クラスで、ItemizedOverlayクラスを使うには、
このクラスを拡張したクラスを使います。

package net.learn2develop.maps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOvelay;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{
  //---array of OverlayItem objects---
  private ArrayList<OverlayItem> mOvelays =
       new ArrayList<OverlayItem>();
  Context mContext;

  public MyItemizedOverlay(Drawable defaultMarker){
   super(boundCenterBottom(defaultMarker));

  public MyItemizedOverlay(Drawable defaultMarker, Context context){
   super(boundCenterBottom(defaultMarker));
   mContext = context;
  }

  //---add an OverlayItem object to the map---
  public void addOverlay(OverlayItem overlay){
   mOverlays.add(overlay);
   //---call this to draw the OverLayItem objects---
   populate();
  }

  //---remove an OverlayItem object from the map---
  public void removeOverlay(OverlayItem overlay){
   mOverlays.remove(overlay);
   //---call this to draw the OverLayItem objects---
   populate();
  }

  //---called when populate() is called; returns each OvelayItem object
  //in the array---
  @Override
  protected OverlayItem createItem(int i){
   return mOverlays.get(i);
  }

  //---return the numbet of OverlayItem objects---
  @Override
  public int size(){
   return mOverlays.size();
  }

  //---called when the user taps on the OverlayItem objects---
  @Override
  protected boolean onTap(int index){
   OverlayItem item = mOberlays.get(index);
   AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
   dialog.setTitle(item.getTitle());
   dialog.setMessage(item.getSnippet());
   dialog.show();
   return true;
  }
}

上のコードは抽象のItemizedOverlayクラスの実行と、
以下のメソッドを再定義します。
●クラスのための2つのコンストラクタ
●addOverlay()メソッドは、クラスにOverlayItemオブジェクトを
追加できるようにします。
●removeOverlay()メソッドは、クラスからOverlayItemオブジェクトを
削除できるようにします。
●createItem()メソッドはクラスに含まれるそれぞれの
OverlayItemオブジェクトを返します。
●size()メソッドはクラスのOverlayItemオブジェクトの
総数を返します。
●onTap()メソッドはユーザがOverlayItemオブジェクトをタップしたら、
いつでもアラートダイアログを表示します。
MyItemizedOverlayクラスのインスタンスを作り、その時に、
いくつかのOverlayItemオブジェクトをこのクラスに追加します。
OverlayItem用のコンストラクタはマーカの位置を示すGeopointをとります。
同じように2つの文字列はタイトルを示し、マーカの記述になります。
MyItemizedOverlayオブジェクトがOverlayItemオブジェクトと
設定されたら、MyItemizedOverlayオブジェクトをMapViewの
オーバレイリストに加えられます。
以下のコードでこれを明確にします。

package net.learn2develop.maps;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity{
  MapView mapView;
  MapController mc;

  GeoPoint p1 = new GeoPoint(ここに緯度、経度を入れます);
  GeoPoint p2 = new GeoPoint(ここに緯度、経度を入れます);

  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   mapView = (MapView) findViewById(R.id.mapView);
   mapView.setBuiltInZoomControls(true);

   mc = mapView.getController();

   //---get your current location and display a blue dot---
   MyLocationOverlay myLocationOverlay =
    new MyLocationOverlay(this, mapView);

   mapView.getOverlays().add(myLocationOverlay);
   myLocationOverlay.enableMyLocation();

   List<Overlay> listOfOverlays = mapView.getOverlays();
   Drawable drawable =
      this.getResources().getDrawable(R.drawable.ic_launcher);

   MyItemizedOverlay itemizedoverlay =
      new MyItemizedOverlay(drawable, this);
   OverlayItem overlayitem1 = new OverlayItem(p1, "Hello Google",
      "I'm an Android");

   //---add an overlayitem---
   itemizedoverlay.addOverlay(overlayitem1);

   OverlayItem overlayitem2 = new OverlayItem(p2, "Hello Google",
      "I'm an swimming");

   //---add an overlayitem---
   itemizedoverlay.addOverlay(overlayitem2);

   //---add the overlay---
   listOfOverlays.add(itemizedoverlay);

   mc.animateTo(p1);
   mc.setZoom(12);
  }

  @Override
  protected boolean isRouteDisplayed(){
  }
}

マーカをタップしたときに、
アラートダイアログが表示され、マーカのタイトルと記述が出てきます。
マップの上に全くマーカを表示させたくないときなどのように、
MyItemizedOverlayオブジェクトから
OverlayItemオブジェクトを削除するには、removeOverlay()メソッドを
使います。

//---remove an overlayitem---
itemizedoverlay.removeOverlay(overlayitem1);

マップからオーバレイ全体を削除するには、Listクラスの
remove()クラスを使います。

//---remove an overlay---
listOfOverlays.remove(itemizedoverlay);

Drawing a Line Connecting Two Points

時には、マップ上に2つのポイントを表示し、その2つのポイントを
線で繋ぎたいこともあるでしょう。
一連のポイントを持ち、たどった事のある一連のポイントの経路を
描き出したい時に役に立ちます。
Google Maps上に線を引くには、Overlayクラス内部のdraw()メソッドを
使います。以下のコードは、draw()メソッドを実行し、
その時マップ上の2つの位置にアプケーションアイコンを配置します。
そして、2つの位置をつなぐ線を引くためのPaintオブジェクトを
セットアップします。

package net.learn2develop.maps;

import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphixs.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

public class MainActivity extends MapActivity{
  MapView mapView;
  MapController mc;

  GeoPoint p1 = new GeoPoint(ここに緯度、経度を入れます);
  GeoPoint p2 = new GeoPoint(ここに緯度、経度を入れます);

  private class MapOverlay extends com.google.android.maps.Overlay{

   @Override
   public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
       long when){
    super.draw(canvas, mapView, shadow);
    Bitmap bmp = BitmapFactory.decodeResource(
     getResources(), R.drawable.ic_launcher);

     //---translate the GeoPoint to screen pixels---
     Point screenPts1 = new Point();
     mapView.getProjection().toPicwls(p1, acreenPts1);

     //---translate the GeoPoint to screen pixels---
     Point screenPts2 = new Point();
     mapView.getProjection().toPixels(p2, screenPts2);

     //---add the first marker---
     canvas.drawBitmap(bmp, screenPts1.x - bmp.getwidth()/2,
         screenPts1.y - bmp.getHeight()/2, null);

     //---add the second marker---
     canvas.drawBitmap(bmp, screenPts2.x - bmp.getwidth()/2,
         screenPts2.y - bmp.getHeight()/2, null);

     Paint Paint = new Paint();
     paint.setStyle(Style, STROKE);
     paint.setColor(RGBコードを入れます);
     paint.setAntiAlias(true);
     paint.setStrokeWidth(5);

     //---draws a line connecting the 2 points---
     canvas.drawLine(screenPts1.x, screenPts1.y, screenPts2.x,
         screenPts2.y, paint);

     return true;
    }
   }

   @Override
   public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.setVuiltInZoomControls(true);

    mc = mapView.getController();

    //---get your current location and display blue dot---
    MyLocationOverlay myLocationOverlay =
        new MyLocationOverlay(this, mapView);

    /*
    //=========================================
    List<Overlay> listOfOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(
        R.drawable.ic_launcher);

    MyItemizedOverlay itemizedoverlay =
        new MyItemizedOverlay(drawable, this);,br />     OverlayItem overlayitem1 = new OverlayItem(
        p1, "Hello Google", "I'm an Android");

    //---add an overlayitem---
    itemizedoverlay.addOverlay(overlayitem1);

    OverlayItem overlayitem2 = new OverlayItem(
        p2, "Hello Google", "I'm swimming");

    //---add an overlayitem---
    itemizedoverlay.addOverlay(overlayitem2);

    //---add the overlay---
    listOfOverlays.add(itemizedoverlay);
    mc.animateTo(p1);
    mc.setZoom(12);
    */

    MapOverlay mapOverlay = new MapOverlaly();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.add(mapOverlay);

    mc.animateTo(p1);
    mc.setZoom(12);
   }

   @Override
   protected boolean isRouteDisplayed(){
    return false;
   }
}

toPixels()メソッドはGeoPointオブジェクトを
スクリーン調整の中で変換し、マップ上にアプリケーションアイコンを
描きます。

ホーム
便利堂ロゴ
inserted by FC2 system