#目的
既存のアプリに地図を埋め込む。
#compile 'com.google.android.gms:play-services:8.4.0'追加
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.******.MainActivity">
<fragment
android:name="com.*******.MapsFragment"
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</RelativeLayout>
fragment_map.xml
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
MapsFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsFragment extends Fragment {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
public MapsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// 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) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.getUiSettings().setZoomControlsEnabled(true);//拡大縮小ボタン表示
LatLng kinkakuzi = new LatLng(35.039352, 135.729265);//金閣寺
CameraPosition.Builder camerapos = new CameraPosition.Builder();//表示位置の作成
camerapos.target(kinkakuzi);//カメラの表示位置の指定
camerapos.zoom(13.0f);//ズームレベル
camerapos.bearing(0);//カメラの向きの指定(北向きなので0)
camerapos.tilt(25.0f);//カメラの傾き設定
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(camerapos.build()));//マップの表示位置変更
MarkerOptions options = new MarkerOptions();//ピンの設定
options.position(kinkakuzi);//ピンの場所を指定
options.title("金閣寺");//マーカーの吹き出しの設定
mMap.addMarker(options);//ピンの設置
}
}
マニフェスト
com.google.android.gms:play-services:8.4.0
android.permission.WRITE_EXTERNAL_STORAGE
GooglePlayサービス SDKのバージョンが、8.3以上の場合は、WRITE_EXTERNAL_STORAGE パーミッションがなくても Google Maps Android API を使用することが出来るようになったみたいです。
android.permission.INTERNET
Google マップサーバーからマップタイルをダウンロードするために API によって使用されます。
ACCESS_COARSE_LOCATION
あいまいな位置情報を取得
ACCESS_FINE_LOCATION
明確な位置情報を取得
glEsVersion="0x00020000" android:required="true"
Google Maps Android API は、OpenGL ES2を使って、マップをレンダリングしているので、OpenGL ES2に対応していない端末からインストールされないようにしないといけない。
android:required="true"にすることで、OpenGL ES バージョン 2 をサポートしていない端末からは、Google Playストア上にアプリを非表示にすることが出来きます。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.*********">
@追加------------------------------------------------
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-feature android:required="true" android:glEsVersion="0x00020000"/>
------------------------------------------------
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
@追加------------------------------------------------
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="**********" />
-------------------------------------------------------
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>