2016-05-20 76 views
0

我試圖在我的片段視圖中顯示一個mapview,我在谷歌控制檯devoloper中創建密鑰,我在我的電腦和所有athers步驟中生成密鑰,但是我alwys得到了w格倫斯科倫,我該如何解決這個問題。谷歌地圖不能在片段視圖中工作

代碼:

家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> 

回答

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定義
  • 類屬性
+0

我總是得到這個監視器'd/OpenGLRenderer:prepareDirty(0.00,0.00,720.00,1280.00)不透明1 <0x6005ca70> 二月5日至21日:15:44.628 28179-28179/com.malek.sos.sosdepanage d/OpenGLRenderer:完成<0x6005ca70>' –