最終更新:
moonlight_aska 2013年09月22日(日) 20:22:55履歴
「Googleマップを表示する」を参照し, Googleマップが表示できるようにする.
レイアウトXMLで, fragmentタグ内にmapから始まるプロパティを利用する.
注) mapプロパティを設定した場合, "Unexpected namespace prefix "map" found for tag fragment"が発生する場合がある.
そのときは, Project > Cleanを実行する.

レイアウトXMLで, fragmentタグ内にmapから始まるプロパティを利用する.
- res/layout/activity_main.xml
- xmlns:mapを指定する.
- map:mapTypeプロパティに, "satellite" or "hybrid"を設定する. (デフォルト"normal")
normal | 道路, 建物や重要な自然の物, 川等が名称とともに表示される |
satellite | 衛星写真 |
hybrid | 衛星写真の上に、道路や建物等と名称が表示される |
terrain | 地形図. 全てではないが、いくつかの道路と名称が表示される |
none | なし |
そのときは, Project > Cleanを実行する.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="35.41"
map:cameraTargetLng="139.41"
map:cameraZoom="10"
map:mapType="hybrid"
/>
</RelativeLayout>
- 動作例

「Googleマップを表示する」を参照し, Googleマップが表示できるようにする.
- MainActivity.java
- getSupportFragmentManagermメソッドで, FragmentManagerのインスタンスを取得する.
- FragmentManager#findFragmentByIdメソッドで, SupportMapFragmentのインスタンスを取得する.
- SupportMapFragment#getMapメソッドで, GoogleMapのインスタンスを取得する.
- GoogleMap#setMapTypeメソッドで, "MAP_TYPE_SATELLITE" or "MAP_TYPE_HYBRID"を設定する.
package com.moonlight_aska.android.googlemapv2;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
SupportMapFragment fragment = (SupportMapFragment)manager.findFragmentById(R.id.map);
GoogleMap map = fragment.getMap();
if (map != null) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
}
}
「Googleマップを表示する」を参照し, Googleマップが表示できるようにする.

- MainActivity.java
- findViewByIdメソッドで, MapViewのインスタンスを取得する.
- MapView#isSatelliteメソッドで, 衛星写真表示モードかチェックする.
- MapView#setSatelliteメソッドで, 衛星写真表示に設定する.
package com.moonlight_aska.android.web.googlemap01;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mview = (MapView)findViewById(R.id.mapview);
// 衛星写真表示に設定
if (!mview.isSatellite()) {
mview.setSatellite(true);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
- 動作例

コメントをかく