2016-12-15 68 views
0

我與谷歌地圖掙扎。我使用的是碎片抽屜,所以我必須AFAIK使用MapView的(從片段中沒有片段調用)。地圖內容顯示不出來使用的MapView從片段

首先我有一個授權失敗,所以我重新生成密鑰,跑密鑰工具,我google_maps_api.xml是正確的那麼關於授權問題的消息已不存在。儘管如此,我仍然沒有看到任何內容,而且我對StrictModeDiskReadViolation有一個奇怪的消息。

這是我在logcat中得到:

12-15 20:41:14.842 703-703/com.bernard_zelmans.check I/Google Maps Android API: Google Play services package version: 10084448 
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
12-15 20:41:14.842 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
12-15 20:41:14.882 703-25272/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 
12-15 20:41:14.882 703-703/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskWriteViolation 
12-15 20:41:14.882 703-25274/com.bernard_zelmans.check W/f: Suppressed StrictMode policy violation: StrictModeDiskReadViolation 

這裏是從我的應用程序快照:

enter image description here

這裏是我的代碼:

public class PortFragment extends Fragment implements OnMapReadyCallback { 
    Context context; 
    View view; 

    MapView mapView; 
    GoogleMap map; 
    LatLng CENTER = null; 

    public LocationManager locationManager; 
    private GoogleMap googleMap; 
    double longitude = 0; 
    double latitude = 0; 

    public PortFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.portscan, null); 
     MapsInitializer.initialize(this.getActivity()); 
     mapView = (MapView) view.findViewById(R.id.mapView); 
     mapView.onCreate(savedInstanceState); 
     mapView.getMapAsync(this); 
     return (view); 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     context = getActivity().getApplicationContext(); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     setUpMap(); 
     googleMap = map; 
     LatLng sydney = new LatLng(-34, 151); 
     googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 

    public void setUpMap() { 
     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     googleMap.setMyLocationEnabled(true); 
     googleMap.setTrafficEnabled(true); 
     googleMap.setIndoorEnabled(true); 
    } 

以下是我的manifest.xml:

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

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.STORAGE" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:configChanges="keyboardHidden|screenSize" 
     android:icon="@mipmap/ic_launcher" 
     android:label="Test" 
     android:logo="@drawable/ic_launcher" 
     android:screenOrientation="portrait" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Activities.Settings" /> 
     <activity android:name=".Activities.About" /> 

     <service 
      android:name=".Service" 
      android:exported="false" /> 

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

     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 


    </application> 

</manifest> 

回答

1

如果你想在你的片段添加地圖,你不應該添加這樣創建它。您應該添加在你的佈局,像你一樣,但隨後在onCreateView()方法,你應該得到的視圖和直接調用getMapAsync它:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    view = inflater.inflate(R.layout.portscan, container);  
    MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    return (view); 

} 

而且在佈局:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

由於你看,你首先在佈局中添加地圖片段,然後在你自己的片段中處理它。

你應該看看這裏的android GMAP文檔:

https://developers.google.com/maps/documentation/android-api/map

+0

我是新來GMAP。如果我理解你,我需要在我的佈局(fragmenthomepage)中添加一個片段。如果我這樣做,它不會爲我的地圖創建一個片段?你什麼時候使用mapview?我會閱讀你寄給我的網址。 – narb

+0

我編輯我的帖子與佈局。 – Yoleth

+0

因爲我使用碎片,我不相信你的方法。我終於使mapview方法起作用了。我錯過了一個mapview.onResume()。不管怎麼說,還是要謝謝你。 – narb