MapViewのサンプルを使ってMapViewの表示を行っていました。
サンプルではFragmentの中でMapActivityを使って実装をしていましたが、
今回はViewの一つとして使いたかったのでMapViewを使ってみました。
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="hoge.example.com.googlemap.MainActivity"> 8 9 <com.google.android.gms.maps.MapView 10 android:id="@+id/mapView" 11 android:layout_width="441dp" 12 android:layout_height="319dp" 13 tools:layout_editor_absoluteX="0dp" 14 tools:layout_editor_absoluteY="0dp"/> 15 16</android.support.constraint.ConstraintLayout>
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="hoge.example.com.googlemap"> 4 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 5 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 6 <uses-permission android:name="android.permission.INTERNET" /> 7 8 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@mipmap/ic_launcher" 13 android:label="@string/app_name" 14 android:roundIcon="@mipmap/ic_launcher_round" 15 android:supportsRtl="true" 16 android:theme="@style/AppTheme"> 17 <meta-data 18 android:name="com.google.android.geo.API_KEY" 19 android:value="@string/google_maps_key" /> 20 <activity android:name=".MainActivity"> 21 <intent-filter> 22 <action android:name="android.intent.action.MAIN"/> 23 24 <category android:name="android.intent.category.LAUNCHER"/> 25 </intent-filter> 26 </activity> 27 </application> 28 29</manifest>
java
1public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { 2 3 private GoogleMap mMap; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10 MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView); 11 mapFragment.getMapAsync(this); // ←ここで落ちる 12 } 13 14 15 @Override 16 public void onMapReady(GoogleMap googleMap) { 17 mMap = googleMap; 18 19 // Add a marker in Sydney and move the camera 20 LatLng sydney = new LatLng(-34, 151); 21 mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 22 CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(35.68, 139.76), 12); 23 mMap.moveCamera(cUpdate); 24 } 25}
こんな感じで実装しましたがアプリが強制終了してしまいます。
よろしければご教授お願いいたします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/07 07:19