Add Google Maps API V2 in a fragment

前端 未结 11 1964
误落风尘
误落风尘 2020-11-29 03:00

I\'m trying to show the map from the Google Maps API V2 in fragment. I tried with the SupportMapFragment, but I can\'t get the expected output. Also I\'m a beginner on this

11条回答
  •  爱一瞬间的悲伤
    2020-11-29 03:42

    Here is the code,

    public class YourFragment extends Fragment {
        // ...
      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
              static final LatLng KIEL = new LatLng(53.551, 9.993);
              private GoogleMap map;
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.yourlayout, null, false);
    
            map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
    
    Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                    .title("Hamburg"));
                Marker kiel = map.addMarker(new MarkerOptions()
                    .position(KIEL)
                    .title("Kiel")
                    .snippet("Kiel is cool")
                    .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
    
                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
    
                // Zoom in, animating the camera.
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    
            //...
    
            return v;
        }
    

    Your layout,

     
    
        
    
    
    

    Make some changes in your manifest file also.Like,

        
    
    
        
    
        
    
        
    
        
        
        
        
        
        
    
        
            
                
                    
    
                    
                
            
    
            
        
    
     
    

提交回复
热议问题