Bonjour,

Je rencontre un soucis concernant l'affichage de marqueurs sur une google map.

En effet, ces derniers ont tendance à clignoter lorsque la localisation de l'utilisateur est activée.

J'ai localisé en partie le soucis, en effet ma mapview est intégrée à un fragment affiché par un viewpager, aussi ce viewpager contient 3 autres fragments similaires (seuls les marqueurs affichés diffèrent). Lorsque je les remplace par des fragments classiques, les marqueurs restent fixes.

Aussi je sais que par défaut, un viewpager charge le fragment courant ainsi que celui adjacent.
La solution serait-elle de court-circuiter le fonctionnement du viewpager ?
J'ai également lu que l'instance d'opengl serait commune à tous ces fragments, cela pourrait-peut-être causé des soucis d'affichage ?

Mon fragment dont héritent chacun des fragments affichant une map :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
public class MapBaseFragment extends BaseFragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
 
private final String TAG = "MapBaseFragment";
private Polyline polyline;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
private GoogleMap googleMap;
private double mLatitude = 50.650;
private double mLongitude = 3.083;
private Fiche fiche;
 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
}
 
public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode) {
    if (polyline != null)
        polyline.remove();
    Map<String, String> map = new HashMap<>();
    map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
    map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
    map.put(GetDirectionsAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
    map.put(GetDirectionsAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
    map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);
 
    GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this, googleMap);
    asyncTask.execute(map);
}
 
public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
    PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.BLUE);
    for (int i = 0; i < directionPoints.size(); i++) {
        rectLine.add(directionPoints.get(i));
    }
    polyline = googleMap.addPolyline(rectLine);
}
 
@Override
public void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}
 
@Override
public void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}
 
@Override
public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected");
 
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
 
    try {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}
 
@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "onConnectionSuspended");
}
 
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d(TAG, "onConnectionFailed " + connectionResult);
}
 
public Location getLastLocation() {
    try {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            Log.d(TAG, "mLastLocation ! =null");
            return mLastLocation;
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return null;
}
 
@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "onLocationChanged");
    mLastLocation = location;
    updateUserLocation(location);
 
}
 
public void updateUserLocation(Location location) {
    Log.d(TAG, "CarteFragment");
    if (!googleMap.isMyLocationEnabled()) {
        try {
            googleMap.setMyLocationEnabled(true);
            Log.d(TAG, "set to true");
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
    googleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
            .title("My Location").icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    if (fiche != null) {
        Log.d(TAG, "fiche != null");
        findDirections(mLatitude, mLongitude, fiche.getLatidude(), fiche.getLongitude(), "walking");
    }
}
 
public GoogleMap getGoogleMap() {
    return googleMap;
}
 
public void setGoogleMap(GoogleMap googleMap) {
    this.googleMap = googleMap;
}
 
public Fiche getFiche() {
    return fiche;
}
 
public void setFiche(Fiche fiche) {
    this.fiche = fiche;
}
 
public double getmLatitude() {
    return mLatitude;
}
 
public void setmLatitude(double mLatitude) {
    this.mLatitude = mLatitude;
}
 
public double getmLongitude() {
    return mLongitude;
}
 
public void setmLongitude(double mLongitude) {
    this.mLongitude = mLongitude;
}
Un des fragments map :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public class CoachSanteFragment extends MapBaseFragment implements OnMapReadyCallback,
    GoogleMap.OnMarkerClickListener,
    CallAPICallbackInterface {
 
private final String TAG = "CoachSanteFragment";
private View rootView;
private MapView mapView;
private HashMap<Marker, Fiche> hashMap = new HashMap<>();
 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_coach_sante, container, false);
    if(((PratiquesActivity) getActivity()).isReload()) {
        ((PratiquesActivity) getActivity()).recreate();
    }
    Log.d(TAG, "onCreateView");
    mapView = (MapView) rootView.findViewById(R.id.fragment_coach_sante_carte_mapview);
    mapView.onCreate(savedInstanceState);
 
    mapView.getMapAsync(this);
 
    initAddingListener();
 
    return rootView;
}
 
@Override
public void onMapReady(GoogleMap googleMap) {
    setGoogleMap(googleMap);
    initGoogleMap();
}
 
@Override
public boolean onMarkerClick(Marker marker) {
    Log.d(TAG, "OnMarkerClick");
    if(marker.getTitle() == null || !marker.getTitle().equals("My Location")) {
        InformationsFragment informationsFragment = new InformationsFragment();
        informationsFragment.setOnItineraireSelected(new InformationsFragment.OnItineraireSelected() {
            @Override
            public void showItineraire(Fiche fiche) {
                setFiche(fiche);
                findDirections(getmLatitude(), getmLongitude(), fiche.getLatidude(), fiche.getLongitude(), "walking");
            }
        });
        Bundle args = new Bundle();
        args.putParcelable("fiche", hashMap.get(marker));
        informationsFragment.setArguments(args);
        informationsFragment.show(getFragmentManager(), null);
    }
    return true;
}
 
@Override
public void apiCallback(Integer responseCode, String responseMessage, List<?> data, Integer delegateCode) {
    Log.d(TAG, "apiCallback");
    if (data != null && data.size() != 0) {
        if (delegateCode == FicheDelegate.DELEGATE_CODE && responseCode == 200) {
            if (data.get(0) instanceof Fiche) {
                initMarkers((List<Fiche>) data);
            }
        }
    }
}
 
private void initGoogleMap() {
    getGoogleMap().setMapType(GoogleMap.MAP_TYPE_HYBRID);
    getGoogleMap().getUiSettings().setZoomControlsEnabled(true);
    getGoogleMap().setOnMarkerClickListener(this);
    MapsInitializer.initialize(this.getActivity());
 
    //Coordonnées de la France
    LatLng latLng = new LatLng(46.227638, 2.213749);
    getGoogleMap().animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5f));
 
    new CallAPI(rootView.getContext(), this,
            new FicheDelegate(rootView.getContext(),
                    "fiche", "Service gastro-pédiatrie",
                    "50.650", "3.083", null
            ))
            .execute();
}
 
@Override
public void onResume() {
    super.onResume();
    mapView.onResume();
}
 
@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}
 
private void initMarkers(List<Fiche> ficheList) {
    Log.d(TAG, "initMarkers");
    Marker marker;
    for (Fiche fiche : ficheList) {
        marker = getGoogleMap().addMarker(new MarkerOptions()
                .position(new LatLng(fiche.getLatidude(), fiche.getLongitude()))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_c_160)));
        hashMap.put(marker, fiche);
    }
}
 
private void initAddingListener() {
    TextView textViewAdding = (TextView) rootView.findViewById(R.id.fragment_coach_sante_textview_add);
    textViewAdding.setText(textViewAdding.getText().toString().toUpperCase());
    final FormulaireCreationPratiquesFragment formulaireCreationPratiquesFragment = new FormulaireCreationPratiquesFragment();
    Bundle args = new Bundle();
    args.putString("formulaireType", "Service gastro-pédiatrie");
    formulaireCreationPratiquesFragment.setArguments(args);
    textViewAdding.setText(textViewAdding.getText().toString().toUpperCase());
    textViewAdding.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((PratiquesActivity) getActivity()).showFormulaire(formulaireCreationPratiquesFragment);
        }
    });
}
L'adapter de mon viewpager :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class PratiquesViewPagerAdapter extends FragmentStatePagerAdapter {
 
private Context context;
 
public PratiquesViewPagerAdapter(Context context, FragmentManager fm) {
    super(fm);
    this.context = context;
}
 
@Override
public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
        case 0 :
            fragment = new HopitauxFragment();
            break;
        case 1:
            fragment = new Fragment();
            //fragment = new DieteticienneFragment();
            break;
        case 2:
            fragment = new Fragment();
            //fragment = new CoachSanteFragment();
            break;
        case 3:
            fragment = new Fragment();
            //fragment = new TabacologueFragment();
            break;
        default :
            fragment = new HopitauxFragment();
            break;
    }
    return fragment;
}
 
@Override
public int getCount() {
    return 4;
}
 
@Override
public CharSequence getPageTitle(int position) {
    String title;
    switch (position) {
        case 0 :
            title = context.getResources().getString(R.string.hopitaux).toUpperCase();
            break;
        case 1 :
            title = context.getResources().getString(R.string.dieteticienne).toUpperCase();
            break;
        case 2 :
            title = context.getResources().getString(R.string.coach_sante).toUpperCase();
            break;
        case 3 :
            title = context.getResources().getString(R.string.tabacologue).toUpperCase();
            break;
        default:
            title = context.getResources().getString(R.string.hopitaux).toUpperCase();
            break;
    }
    return title;
}
 
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
 
    if (position <= getCount()) {
        FragmentManager manager = ((Fragment) object).getFragmentManager();
        FragmentTransaction trans = manager.beginTransaction();
        trans.remove((Fragment) object);
        trans.commit();
    }
}
Ma dépendance gradle :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
compile 'com.google.android.gms:play-services-maps:8.4.0'
En bonus un record du bug : http://fr.tinypic.com/r/259flnn/9

Merci d'avances pour votre temps