IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

Map google et navigation drawer


Sujet :

API standards et tierces Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 6
    Par défaut Map google et navigation drawer
    Bonsoir à toutes et à tous,

    Je débute dans la programmation android et je bloque sur un truc. J'ai fais une application avec un navigation drawer et lorsque l'on appuie sur un des boutons du drawer, je change mon fragment principal pour y mettre une map google. Jusque là tout fonctionne.
    Mais quand j'implémente un clicklistener dans la fonction "onMapReady" (je crée juste un marqueur à l'endroit où j'appuie) rien ne se passe et je ne comprends vraiment pas pourquoi. J'ai testé ma fonction dans un autre programme où il n'y avait pas de navigation drawer et tout fonctionne sans problème.

    Voici le code de mon MapsFragment:

    Code Java : 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
    public class MapsFragment extends Fragment implements OnMapReadyCallback{
     
        private GoogleMap mMap;
        private UiSettings mUiSettings;
     
        public MapsFragment()
        {
     
        }
     
        public static MapsFragment newInstance() {
            return new MapsFragment();
        }
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
            if (mapFragment != null) {mapFragment.getMapAsync(this);}
            return inflater.inflate(R.layout.fragment_map,container, false);
        }
     
        @Override
        public void onMapReady(GoogleMap m)
        {
            mMap=m;
            mUiSettings = mMap.getUiSettings();
            mUiSettings.setMapToolbarEnabled(false);
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    Toast.makeText(getContext(), "Clic détecté", Toast.LENGTH_SHORT).show();
                    mMap.addMarker(new MarkerOptions()
                            .position(latLng)
                            .draggable(true));
                }
            });
        }
    }

    Merci d'avance pour vos réponses,

    Pierre

  2. #2
    Membre chevronné Avatar de Drowan
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2014
    Messages
    460
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2014
    Messages : 460
    Par défaut
    Est-ce que tu ferme bien ton Navigation Drawer ?
    Sinon peut-etre qu'il est encore ouvert et que les clicks soit reçu par le drawer et non par ton Map fragment.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 6
    Par défaut
    Qu'entendez vous par fermer le navigation drawer ?
    Ce qui est étrange c'est que j'ai d'autres fragment dont un avec un bouton et là il n'y pas de soucis pour détecter l'appui.

  4. #4
    Membre chevronné Avatar de Drowan
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2014
    Messages
    460
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2014
    Messages : 460
    Par défaut
    Par fermer, j'entends l'appel de la méthode closeDrawers comme indiqué dans le guide Android
    Je dis ça sans savoir si c'est vraiment la solution, mais la description de ton problème m'a fait penser à une histoire de premier/arrière plan. Comme si ton fragment s'ouvre bien mais est en "arrière" plan et que c'est donc ton drawer qui reçoit les clicks. (donc quand il n'y a pas de drawer, le truc marche. A voir...

    Avec un autre fragment ça marche ? là c'est étrange effectivement. Puex-tu poster le code qui lance le fragment ? Tu es sûr de lancer la map fragment et le bouton fragment de la même manière ?

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2015
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2015
    Messages : 6
    Par défaut
    J'appelle bien la méthode que vous citez, c'est étrange.

    Voici le code de mon activité principale:
    Code Java : 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
    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, StopFragment.OnButtonClickedListener {
     
        private Toolbar toolbar;
        private DrawerLayout drawerLayout;
        private NavigationView navigationView;
     
        private Fragment fragmentNews;
        private Fragment fragmentProfile;
        private Fragment fragmentMap;
     
        private static final int FRAGMENT_NEWS=0;
        private static final int FRAGMENT_PROFILE=1;
        private static final int FRAGMENT_MAPS=2;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            this.configureToolbar();
     
            this.configureDrawerLayout();
     
            this.configureNavigationView();
     
            this.showFirstFragment();
        }
     
        @Override
        public void onBackPressed()
        {
            if (this.drawerLayout.isDrawerOpen(GravityCompat.START))
            {
                this.drawerLayout.closeDrawer(GravityCompat.START);
            }
            else {
                super.onBackPressed();
            }
        }
     
        @Override
        public boolean onNavigationItemSelected(MenuItem item)
        {
            int id = item.getItemId();
     
            switch(id)
            {
                case R.id.activity_main_drawer_news:
                    this.showFragment(FRAGMENT_NEWS);
                    break;
                case R.id.activity_main_drawer_profile:
                    this.showFragment(FRAGMENT_PROFILE);
                    break;
                case R.id.activity_main_drawer_settings:
                    this.showFragment(FRAGMENT_MAPS);
                    break;
                default:
                    break;
            }
            this.drawerLayout.closeDrawer(GravityCompat.START);
     
            return true;
        }
     
        private void showFirstFragment()
        {
            Fragment visibleFragment = getSupportFragmentManager().findFragmentById(R.id.main_frame_layout);
     
            if (visibleFragment == null){
                this.showFragment(FRAGMENT_NEWS);
                this.navigationView.getMenu().getItem(0).setChecked(true);
     
            }
     
        }
     
        @Override
        public void onButtonClicked(View v)
        {
            Intent intent = new Intent(this, EventActivity.class);
            startActivity(intent);
        }
     
        private void showFragment(int fragmentIdentifier)
        {
            switch(fragmentIdentifier)
            {
                case FRAGMENT_NEWS:
                    if (this.fragmentNews == null) this.fragmentNews = NewsFragment.newInstance();
                    if (!fragmentNews.isVisible()){
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.main_frame_layout, fragmentNews).commit();
                    }
                    break;
                case FRAGMENT_PROFILE:
                    if (this.fragmentProfile == null) this.fragmentProfile = StopFragment.newInstance();
                    if (!fragmentProfile.isVisible()){
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.main_frame_layout, fragmentProfile).commit();
                    }
                    break;
                case FRAGMENT_MAPS:
                    if (this.fragmentMap == null) this.fragmentMap = MapsFragment.newInstance();
                    if (!fragmentMap.isVisible()) {
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.main_frame_layout, fragmentMap).commit();
                    }
            }
        }
     
     
        private void configureToolbar()
        {
            this.toolbar = (Toolbar)findViewById(R.id.main_toolbar);
            setSupportActionBar(toolbar);
        }
     
        private void configureDrawerLayout()
        {
            this.drawerLayout = (DrawerLayout)findViewById(R.id.activity_main_drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
            drawerLayout.addDrawerListener(toggle);
            toggle.syncState();
        }
     
        private void configureNavigationView()
        {
            this.navigationView = (NavigationView)findViewById(R.id.main_nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    }

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut
    Bonjour,

    je vérifierai en premier si onMapReady est bien appelée en y mettant un breakpoint. Si non, ca signifie que ton mapFragment est null, donc non créé, parce que onMapReady est appelée par lz méthode getMapAsync. Dans ce cas, je te suggère de sortir ces 2 lignes
    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
            SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
            if (mapFragment != null) {mapFragment.getMapAsync(this);}

    pour les mettre dans la méthode onViewCreated

    Code Java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void onViewCreated (View view, 
                    Bundle savedInstanceState) {
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().view.findFragmentById(R.id.mapFragment); //faudra p'tet rajouter des () : ((SupportMapFragment) getChildFragmentManager().view)
            if (mapFragment != null) {mapFragment.getMapAsync(this);}
    }

Discussions similaires

  1. Rechercher de text sur Map Google
    Par frAydjwe dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 26/05/2011, 14h34
  2. Réponses: 9
    Dernier message: 22/02/2011, 16h07
  3. Plugin gdata maps (Google Maps) pour Eclipse.
    Par Julien411 dans le forum Eclipse Java
    Réponses: 7
    Dernier message: 09/10/2010, 16h39
  4. [FPDF] Static map google
    Par Darkcristal dans le forum Bibliothèques et frameworks
    Réponses: 10
    Dernier message: 22/03/2010, 16h46
  5. Changement de map Google/Bing/Yahoo/etc
    Par sepherseth dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 22/03/2010, 16h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo