はじめに
GoogleMapのマップタイプを変更するサンプルです。
マップタイプは以下の5種類があります。
NONE 地図の非表示
ROADMAP 道路や建物などが表示される地図です
SATELLITE 衛星写真を使った地図です
HYBRID ROADMAPとSATELLITEの複合した地図です
TERRAIN 地形情報を使った地図です
サンプル
簡単なサンプルです。
事前にAPIキーを取得してAndroidManifest.xmlに追加しておいてください。
またbuild.gradleにもimplementationしておいてください。
MapActivity.class
package hoge.example.com.googlemapsample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback, View.OnClickListener{
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.map_none).setOnClickListener(this);
findViewById(R.id.map_normal).setOnClickListener(this);
findViewById(R.id.map_hybrid).setOnClickListener(this);
findViewById(R.id.map_statellite).setOnClickListener(this);
findViewById(R.id.map_terrain).setOnClickListener(this);
// GoogleMapのフラグメント設定
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(35.681167, 139.767052);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom(sydney, 12);
mMap.moveCamera(cUpdate);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.map_none: mMap.setMapType(GoogleMap.MAP_TYPE_NONE); break;
case R.id.map_normal: mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); break;
case R.id.map_hybrid: mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); break;
case R.id.map_statellite: mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); break;
case R.id.map_terrain: mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);; break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/map_none"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="NONE"/>
<Button
android:id="@+id/map_normal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="NORMAL"
android:textSize="10sp"/>
<Button
android:id="@+id/map_hybrid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HYBRID"
android:textSize="10sp"/>
<Button
android:id="@+id/map_statellite"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SATELLITE"
android:textSize="10sp"/>
<Button
android:id="@+id/map_terrain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TERRAIN"
android:textSize="10sp"/>
</LinearLayout>
<!-- GoogleMap表示用フラグメント -->
<fragment
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>