mercredi 25 février 2015

Android MapView Map V2 within Fragment



I have been trying to get my Maps working, I have FragmentTabHost atm only contains 3 tabs. In my Maps tab, it should load the google map fragment and zoom into the users location however it just loads the map but does not do the zoom in method. Also is there I fix if there is a Binrary XML file due to map ID duplicates it crashes the app when I return back to the Maps tab.


Another question is that what method can I take to be able to load the maptab asynchronously.


Map.java



public class Maps extends Fragment {

private GoogleMap googleMap;
MapView mapView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_maps, container, false);

mapView = (MapView) view.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);

// Gets to GoogleMap from the MapView and does initialization stuff
googleMap = mapView.getMap();
MapsInitializer.initialize(this.getActivity());
setUpMapIfNeeded()
return view;
}

@Override
public void onResume() {
mapView.onResume();
super.onResume();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (googleMap == null) {
// Try to obtain the map from the SupportMapFragment.
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview))
.getMap();
// Check if we were successful in obtaining the map.
if (googleMap != null) {
executeMap();
}
}
}

private void executeMap() {
googleMap.setMyLocationEnabled(true);
googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

//Get location services from Google services and choose the best fit through criteria
LocationManager locationMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria bestFit = new Criteria();

//Get name of best provider
String provider = locationMan.getBestProvider(bestFit, true);
//Get Location
Location myLocation = locationMan.getLastKnownLocation(provider);

//Get long and lat, create an object marker
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng position = new LatLng(latitude, longitude);

//Zoom into current location
googleMap.animateCamera(CameraUpdateFactory.newLatLng(position));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(18));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here"));
}


}


XML file



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/mapfrag">

<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>



Aucun commentaire:

Enregistrer un commentaire