public class MapFragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
if(status != ConnectionResult.SUCCESS) {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,activity,requestCode);
dialog.show();
} else {
SupportMapFragment supportMapFragment = (SupportMapFragment) ((MyActivity) activity).getSupportFragmentManager().findFragmentById(R.id.map);
map = supportMapFragment.getMap();
CurListener = new CurrentLocationListener();
map.setMyLocationEnabled(true);
locationTag = true;
map.setOnMyLocationChangeListener(CurListener);
}
return view;
}
private class CurrentLocationListener implements GoogleMap.OnMyLocationChangeListener{
@Override
public void onMyLocationChange(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
if(locationTag) {
LatLng latLng = new LatLng(lat, lng);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(16));
locationTag = false;
}
Log.d(TAG,"lat : " + lat + ", lng : " + lng);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = ((MyActivity)activity).getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.map));
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
@Override
public void onDetach() {
super.onDetach();
}
}
google map v2 에서는 사용자 위치값 업데이트 될때마다 호출되는 리스너를 setOnMyLocationChangeListener 이것만 사용 가능한가요? requestLocationUpdates 와 Criteria를 사용하면 구글맵이 실행될때 현재위치로 가지 않더라구요.