Add Google Maps API V2 in a fragment

前端 未结 11 1960
误落风尘
误落风尘 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:38

    in addition to the answer above, I also had to put the following lines to my manifest

    <!-- inside <aplication> tag -->    
    <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
    

    And I also changed the layout to use SupportMapFragment instead of MapFragment

    <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    
    0 讨论(0)
  • 2020-11-29 03:41

    If you are using android studio create google mapsactivity its default map fragment and generate your map API KEY and do your stuff......

    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this);

    and our override method

    public void onMapReady(GoogleMap googleMap) {

       mMap = googleMap;
       LatLng map = new LatLng(lat, lon);
       mMap.addMarker(new MarkerOptions().position(map).title("your title"));
       mMap.animateCamera(CameraUpdateFactory.newLatLng(map));
       mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    
    }
    
    0 讨论(0)
  • 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,

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    
    </RelativeLayout>
    

    Make some changes in your manifest file also.Like,

        <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.myapp.android.locationapi.maps"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="17"
            android:targetSdkVersion="17" />
    
        <permission
            android:name="com.myapp.android.locationapi.maps.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
    
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />
    
        <uses-permission android:name="com.myapp.android.locationapi.maps.permission.MAPS_RECEIVE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.myapp.android.locationapi.maps.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="your_apikey" />
        </application>
    
    </manifest> 
    
    0 讨论(0)
  • 2020-11-29 03:42
    public class DemoFragment extends Fragment {
    
    
    MapView mapView;
    GoogleMap map;
    LatLng CENTER = null;
    
    public LocationManager locationManager;
    
    double longitudeDouble;
    double latitudeDouble;
    
    String snippet;
    String title;
    Location location;
    String myAddress;
    
    String LocationId;
    String CityName;
    String imageURL;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater
                    .inflate(R.layout.fragment_layout, container, false);
    
        mapView = (MapView) view.findViewById(R.id.mapView);
            mapView.onCreate(savedInstanceState);
    
      setMapView();
    
    
     }
    
     private void setMapView() {
        try {
            MapsInitializer.initialize(getActivity());
    
            switch (GooglePlayServicesUtil
                    .isGooglePlayServicesAvailable(getActivity())) {
            case ConnectionResult.SUCCESS:
                // Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT)
                // .show();
    
                // Gets to GoogleMap from the MapView and does initialization
                // stuff
                if (mapView != null) {
    
                    locationManager = ((LocationManager) getActivity()
                            .getSystemService(Context.LOCATION_SERVICE));
    
                    Boolean localBoolean = Boolean.valueOf(locationManager
                            .isProviderEnabled("network"));
    
                    if (localBoolean.booleanValue()) {
    
                        CENTER = new LatLng(latitude, longitude);
    
                    } else {
    
                    }
                    map = mapView.getMap();
                    if (map == null) {
    
                        Log.d("", "Map Fragment Not Found or no Map in it!!");
    
                    }
    
                    map.clear();
                    try {
                        map.addMarker(new MarkerOptions().position(CENTER)
                                .title(CityName).snippet(""));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    map.setIndoorEnabled(true);
                    map.setMyLocationEnabled(true);
                    map.moveCamera(CameraUpdateFactory.zoomTo(5));
                    if (CENTER != null) {
                        map.animateCamera(
                                CameraUpdateFactory.newLatLng(CENTER), 1750,
                                null);
                    }
                    // add circle
                    CircleOptions circle = new CircleOptions();
                    circle.center(CENTER).fillColor(Color.BLUE).radius(10);
                    map.addCircle(circle);
                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
                }
                break;
            case ConnectionResult.SERVICE_MISSING:
    
                break;
            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
    
                break;
            default:
    
            }
        } catch (Exception e) {
    
        }
    
    }
    

    in fragment_layout

    <com.google.android.gms.maps.MapView
                    android:id="@+id/mapView"
                    android:layout_width="match_parent"
                    android:layout_height="160dp"                    
                    android:layout_marginRight="10dp" />
    
    0 讨论(0)
  • 2020-11-29 03:44

    I've the same issue , I use this code but I still get this error : unable to instantiate fragment com.google.android.gsm.SupportMapFragment: make sure class name exists ,is public, and has empty construct or that is public

    I solved it from here : How to put Google Maps V2 on a Fragment Using ViewPager

    0 讨论(0)
提交回复
热议问题