Last active
April 10, 2017 08:12
-
-
Save LuckOfWise/4959843 to your computer and use it in GitHub Desktop.
MapFragmentをTabの要素として表示したい to @shikajiro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kickhost.eventnavi; | |
import java.util.ArrayList; | |
import jp.kickhost.localsearch.model.Event; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.text.format.Time; | |
import android.util.Log; | |
import android.view.InflateException; | |
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.GoogleMap.OnCameraChangeListener; | |
import com.google.android.gms.maps.MapFragment; | |
import com.google.android.gms.maps.UiSettings; | |
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 EventMapFragment extends Fragment { | |
private GoogleMap mMap; | |
private static View view; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
// ここでハマった | |
// 困ったときのstackoverflow | |
// http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi | |
if (view != null) { | |
ViewGroup parent = (ViewGroup) view.getParent(); | |
if (parent != null) | |
parent.removeView(view); | |
} | |
try { | |
view = inflater.inflate(R.layout.fragment_event_map, container, false); | |
} catch (InflateException e) { | |
/* map is already there, just return view as it is */ | |
} | |
return view; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
setUpMapIfNeeded(); | |
} | |
private void setUpMapIfNeeded() { | |
if (mMap == null) { | |
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragmentMap)).getMap(); | |
if (mMap != null) { | |
setUpMap(); | |
} | |
} | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.87365, 151.20689), 14.0f)); | |
} | |
// とりあえず適当にピンたててみたり。 | |
private void setUpMap() { | |
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); | |
mMap.addMarker(new MarkerOptions().position(new LatLng(-33.87365, 151.20689)).title("Marker")); | |
mMap.addMarker(new MarkerOptions().position(new LatLng(-33.57365, 151.20689)).title("Marker")); | |
// 現在位置表示の有効化 | |
mMap.setMyLocationEnabled(true); | |
// 設定の取得 | |
UiSettings settings = mMap.getUiSettings(); | |
// コンパスの有効化 | |
settings.setCompassEnabled(true); | |
// 現在位置に移動するボタンの有効化 | |
settings.setMyLocationButtonEnabled(true); | |
// ズームイン・アウトボタンの有効化 | |
settings.setZoomControlsEnabled(true); | |
// 回転ジェスチャーの有効化 | |
settings.setRotateGesturesEnabled(false); | |
// スクロールジェスチャーの有効化 | |
settings.setScrollGesturesEnabled(true); | |
// Tlitジェスチャー(立体表示)の有効化 | |
settings.setTiltGesturesEnabled(false); | |
// ズームジェスチャー(ピンチイン・アウト)の有効化 | |
settings.setZoomGesturesEnabled(true); | |
mMap.setOnCameraChangeListener(new OnCameraChangeListener() { | |
@Override | |
public void onCameraChange(CameraPosition position) { | |
Log.d("TEST", "zoop:" + position.zoom); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/linearLayoutContents" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<fragment | |
android:id="@+id/fragmentMap" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
class="com.google.android.gms.maps.MapFragment" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.kickhost.eventnavi; | |
import java.util.ArrayList; | |
import jp.kickhost.localsearch.model.Event; | |
import jp.kickhost.localsearch.network.LocalSearchRestClient; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.app.ActionBar; | |
import android.app.ActionBar.Tab; | |
import android.app.ActionBar.TabListener; | |
import android.app.Activity; | |
import android.app.FragmentTransaction; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import com.loopj.android.http.JsonHttpResponseHandler; | |
import com.loopj.android.http.RequestParams; | |
public class MainActivity extends Activity { | |
ActionBar mActionBar; | |
EventListFragment mListFragment; | |
EventMapFragment mMapFragment; | |
/** | |
* The serialization (saved instance state) Bundle key representing the | |
* current tab position. | |
*/ | |
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mActionBar = getActionBar(); | |
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); | |
TabListener tabListener = new ActionBar.TabListener() { | |
@Override | |
public void onTabSelected(Tab tab, FragmentTransaction ft) { | |
if (tab.getText().equals("Map")) { | |
if (mMapFragment == null) { | |
mMapFragment = new EventMapFragment(); | |
ft.add(android.R.id.content, mMapFragment, "map"); | |
} else { | |
ft.attach(mMapFragment); | |
} | |
} else { | |
if (mListFragment == null) { | |
mListFragment = new EventListFragment(); | |
ft.add(android.R.id.content, mListFragment, "list"); | |
} else { | |
ft.attach(mListFragment); | |
} | |
} | |
} | |
@Override | |
public void onTabReselected(Tab tab, FragmentTransaction ft) { | |
} | |
@Override | |
public void onTabUnselected(Tab tab, FragmentTransaction ft) { | |
if (tab.getText().equals("Map")) { | |
if (mMapFragment != null) { | |
ft.detach(mMapFragment); | |
} | |
} else { | |
if (mListFragment != null) { | |
ft.detach(mListFragment); | |
} | |
} | |
} | |
}; | |
mActionBar.addTab(mActionBar.newTab().setText("List").setTabListener(tabListener)); | |
mActionBar.addTab(mActionBar.newTab().setText("Map").setTabListener(tabListener)); | |
getEventList(); | |
} | |
@Override | |
public void onRestoreInstanceState(Bundle savedInstanceState) { | |
// Restore the previously serialized current tab position. | |
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { | |
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
// Serialize the current tab position. | |
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment