J'utilise des fragments dans mon application et je suis nouveau pour cela. Il y a un fragment dans lequel je souhaite afficher Google Map et obtenir son objet. Pour cela, j'ai un fragment dans mon XML et je souhaite le gonfler dans le fragment lui-même. Lorsque j'essaie de gonfler la vue cartographique, l'erreur d'affichage de cette erreur s'affiche pour getSupportFragmentManager ().
Comment puis-je obtenir l'objet de carte alors c'est le problème principal. Mon XML est comme ça: -
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="250dp"
class="com.google.Android.gms.maps.SupportMapFragment" />
Et mon fragment dans lequel je veux obtenir l'objet Google Map est comme ça: -
public class FindMyCar extends Fragment {
private TextView mTvFind;
private TextView mTvPark;
private EditText mEtParkHint;
private GoogleMap map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.findmycar, null);
initViews(view);
Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf");
mTvFind.setTypeface(type);
mTvPark.setTypeface(type);
mEtParkHint.setTypeface(type);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void initViews(View view){
mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
}
Quelqu'un peut-il m'aider à obtenir l'objet pour ma carte afin que je puisse montrer l'emplacement actuel et dessiner le marqueur à cet endroit?.
Toute aide serait appréciable ... Merci d'avance.
Essaye ça
GoogleMap map = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
et dans votre xml
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
class="com.google.Android.gms.maps.SupportMapFragment" />
La méthode getMap () est privée
La méthode getMap()
est dépréciée, mais une fois le play-services 9.2
it supprimé, il vaut donc mieux utiliser getMapAsync (). Vous pouvez toujours utiliser getMap () uniquement si vous ne souhaitez pas mettre à jour le play-services 9.2
pour votre application.
Pour utiliser getMapAsync()
, implémentez l'interface OnMapReadyCallback
sur votre activité ou votre fragment:
Pour fragment
public class MapFragment extends Android.support.v4.app.Fragment
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
Pour l'activité
public class MapActivity extends FragmentActivity
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
Fragment en xml
<fragment xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
Essayez ceci cela fonctionne pour moi Comment mettre Google Maps V2 sur un fragment utilisant ViewPager
<Fragment
Android:id="@+id/map"
Android:layout_width="wrap_content"
Android:layout_height="match_parent"
class="com.google.Android.gms.maps.SupportMapFragment" />
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
Essayez ceci je l'ai fait comme échantillon selon votre besoin
public class LocationMapActivity extends FragmentActivity {
private GoogleMap mMap;
static boolean Iscamera = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.locatio_map);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getMaxZoomLevel();
mMap.getMinZoomLevel();
mMap.getUiSettings();
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.animateCamera(CameraUpdateFactory.zoomOut());
mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(arg0.getLatitude(), arg0
.getLongitude()))
.title("I am Here!!")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.map_icon)));
if (!Iscamera) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(arg0.getLatitude(), arg0
.getLongitude()), 14));
Iscamera = true;
}
try {
if (Constant.FORTNAME.equals("Arad Fort")) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.2525, 50.6269))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Bahrain Fort")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.2333598,
50.52035139))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("International Circuit")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.0303251,
50.51121409))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("Khamis Mousque")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.158719, 50.516426))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("king fahad causeway")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.1723987,
50.4579942))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Riffa Fort")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.11771965026855,
50.56298065185547))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("Royal Golf Club")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.13000, 50.55500))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Tree of life")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(25.9940396,
50.583135500000026))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
}
}
}
// ********************************************************************************************
@SuppressWarnings("unused")
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
"Marker"));
}
}
public class MapActivity extends FragmentActivity
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
Vous pouvez utiliser ceci pour créer une carte, mais il retournera que l'objet de carte est null car vous ne trouvez pas de solution avec getMapAsync pour créer un nouvel objet mMap. Ce n'est que la tâche en arrière-plan avec return null
Code révisé:
Votre fichier XML -
<fragment
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
Votre fichier Java (uniquement le code pertinent) -
View view = null, mapView = null;
private GoogleMap mMap;
view = inflater.inflate(R.layout.findmycar, container,false);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
.findFragmentById(R.id.map);
mMap = supportMapFragment.getMap();
mapView = (View) view.findViewById(R.id.map);