2013年5月9日木曜日

Google Map v2


Google Map が v2 になってとても簡単になっていた。
また、サポートされなくなるので、変更しておいたほうがよい。

これを使うには、Google Play Services ライブラリをDLして、google-play-services_libフォルダをEclipceにImportし、
使用するプロジェクトでlibとしてリンクする必要がある。(Preferences-Android)

(例)
package com.example.mapv2text;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.support.v4.app.FragmentActivity;

public class MainActivity  extends FragmentActivity {

private GoogleMap mMap;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    protected void onResume() {
        super.onResume();    
   // Do a null check to confirm that we have not already instantiated the map.
   if (mMap == null) {
       // Try to obtain the map from the SupportMapFragment.
       mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
               .getMap();
        mMap.setMyLocationEnabled(true); //My Locationボタンを表示する
       
        //Mapの中心とZoomを指定して表示
           //LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
           LatLng latLng = new LatLng(36.242612, 139.553061);
           CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 13);

        mMap.moveCamera(update);
         
       // マーカー表示
       if (mMap != null) {
        mMap.addMarker(new MarkerOptions()
        .position(new LatLng(36.242612, 139.553061))
        .title("タイトル")
        .snippet("ここはどこでしょう?")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))
        );
         
       }
   }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

1 件のコメント:

  1. 自己レス
    ADTを22にアップしたら、

    java.lang.NoClassDefFoundError

    が出るようになった。
    ネットでいろいろ探してみたが、なかなか解決せず。

    結論は、プロジェクトのPropertiesで、Java Build Path の Order and Export で、 Android Private Librariesにチェックを付けたら直った。

    返信削除