谷歌地图在片段视图中不起作用

时间:2016-05-20 23:37:32

标签: android google-maps android-fragments

我尝试在我的片段视图中显示一个mapview,我在谷歌控制台devoloper中创建了密钥,并且我在我的电脑和所有的步骤中生成了密钥,但我仍然可以获得wren scren,我该如何解决这个问题。

代码:

home fragment.java

package com.malek.sos.sosdepanage;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    MapView mapView;
    GoogleMap map;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        map = mapView.getMap();
      /*  map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);
*/
        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls

            MapsInitializer.initialize(this.getActivity());


        // Updates the location and zoom of the MapView
       /* CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
        map.animateCamera(cameraUpdate);*/
        return v;
    }

}

片段家庭布局

<FrameLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    android:background="@color/white"
    tools:context="com.malek.sos.sosdepanage.HomeFragment"
    android:name="com.google.android.gms.maps.SupportMapFragment" >

    <!-- TODO: Update blank fragment layout -->
    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.malek.sos.sosdepanage">

    <uses-permission android:name="android.permission.INTERNET"/>
    <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"/>

    <permission
        android:name="com.example.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyC7esM0vElp3uNvgj6AgrWjuyQmAG_G1ac"/>

        <activity android:name=".MainActivity" android:theme="@style/AppTheme"/>
        <activity android:name=".LoginActivity" android:theme="@style/AppTheme"/>
        <activity android:name=".HomeActivity" android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

活动类

public class MapActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        initMap();
    }

    private void initMap() {
        MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        map.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        // get latitude & longitude from somewhere
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
    }

}

布局xml文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        map:cameraTargetLat="-38.004635"
        map:cameraTargetLng="-57.541511"
        map:cameraZoom="14"
        map:mapType="normal"
        map:uiCompass="false"
        map:uiRotateGestures="false"
        map:uiScrollGestures="false"
        map:uiTiltGestures="false"
        map:uiZoomControls="false"
        map:uiZoomGestures="false"
        tools:ignore="MissingPrefix" />

</LinearLayout>

重要的部分是:

  • 实施OnMapReadyCallback
  • 片段xml定义中的class属性