2017-02-07 11 views
0

FragmentでGoogleマップが必要ですが、setMyLocationとマーカーが付いていますが、アンドロイド6には私の位置ボタンとマーカーが表示されていません。 Android 4,5ではすべてがうまく見えます。フラグメント上のGoogleマップのみ地図android 6+

XML:  
    <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="com.team.noty.carowner.fragment.Map"> 


    <Spinner 
     android:id="@+id/tech_centre_spinner_map" 
     android:entries="@array/Услуги" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" /> 

    <Spinner 
     android:id="@+id/tech_centre_spinner_mark_map" 
     android:entries="@array/Марки" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 

     android:layout_toRightOf="@+id/tech_centre_spinner_map" 
     /> 

    <com.google.android.gms.maps.MapView 
     android:layout_marginTop="5dp" 
     android:layout_below="@+id/tech_centre_spinner_map" 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    </RelativeLayout> 

フラグメント:誰もが必要な場合は

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


     View view = inflater.inflate(R.layout.fragment_map, container, 
    false); 

     mMapView = (MapView) view.findViewById(R.id.map); 
     mMapView.onCreate(savedInstanceState); 

     service = (Spinner) 
    view.findViewById(R.id.tech_centre_spinner_map); 
     mark = (Spinner) 
    view.findViewById(R.id.tech_centre_spinner_mark_map); 

     mMapView.onResume(); // needed to get the map to display 
    immediately 

     typeSpinner(); 
     speckSpinner(); 

     try { 

    MapsInitializer.initialize(getActivity().getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 



      mMapView.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap mMap) { 
        googleMap = mMap; 

        if (ActivityCompat.checkSelfPermission(getActivity(), 
    Manifest.permission.ACCESS_FINE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED && 
    ActivityCompat.checkSelfPermission(getActivity(), 
    Manifest.permission.ACCESS_COARSE_LOCATION) != 
    PackageManager.PERMISSION_GRANTED) { 

         return; 
        } 
        googleMap.setMyLocationEnabled(true); 

        LatLng sydney = new LatLng(-49.1222, 34.2322); 
        googleMap.addMarker(new 
    MarkerOptions().position(sydney).title("Marker Title").snippet("Marker 
    Description")); 

        // For zooming automatically to the location of the 
    marker 
        CameraPosition cameraPosition = new 
    CameraPosition.Builder().target(sydney).zoom(12).build(); 


googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosit  ion)); 


       } 
      }); 


     return view; 

    } 
+0

は、あなたのアプリにアクセス位置の許可を与えていますか?あなたの 'onMapReady'が' googleMap.setMyLocationEnabled(true); 'に達していないようです。 Android 6では、ユーザーが手動で権限を取り消すことができることを考慮してください。 – antonio

+0

私はそれを確認することができるジェネリックを使用していますか? – Azarnoy

+0

設定 - >アプリ - >あなたのアプリ - >権限に移動できます – antonio

答えて

0

私はansverを見つける:

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 

    mMapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap mMap) { 
      googleMap = mMap; 


      //*** Permission StrictMode 
      if (android.os.Build.VERSION.SDK_INT > 9) { 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
       StrictMode.setThreadPolicy(policy); 

      } 

      if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

       return; 
      } 




       mMap.setMyLocationEnabled(true); 



      locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 

      if (!locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER)) { 
       CameraPosition cameraPosition = new CameraPosition.Builder() 
         .target(new LatLng(55.751244, 37.618423)) 
         .zoom(12) 
         .bearing(0) 
         .tilt(20) 
         .build(); 
       CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition); 

       googleMap.animateCamera(cameraUpdate); 
      }else{ 
       CameraPosition cameraPosition = new CameraPosition.Builder() 
         .target(new LatLng(55.751244, 37.618423)) 
         .zoom(13) 
         .bearing(0) 
         .tilt(20) 
         .build(); 
       CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition); 

       googleMap.animateCamera(cameraUpdate); 
      } 


     } 
    }); 


    } else { 

     ActivityCompat.requestPermissions(getActivity(), 
       new String[] { 
         Manifest.permission.ACCESS_COARSE_LOCATION, 
         Manifest.permission.ACCESS_FINE_LOCATION 
       }, 
       MY_LOCATION_REQUEST_CODE); 

    } 
関連する問題