1

悲しいです!たくさんの努力と検索の後、私は自分の問題を見つけることができませんでした。私はGoogleマップで現在の場所を検索したいが、私はまだエラー(必要な権限)を取得しています。setMyLocationEnableのために拒否される可能性があるコール許可android

呼び出しがユーザーによって拒否することができる権限が必要です。コードは許可がgMap.setMyLocationEnable(true);電位のセキュリティ例外 "

を扱う利用可能(チェック 許可を得て)、または明示的であるかどうかを確認する明示的 必要があります誰か教えてください。どうすれば修正できますか?私のコードは以下の通りです。

AddMasjid.java

package com.example.saroosh.masjidnow.Tabs; 

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

import com.example.saroosh.masjidnow.R; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.MapView; 
import com.google.android.gms.maps.MapsInitializer; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class AddMasjid extends Fragment implements OnMapReadyCallback{ 

    MapView gMapView; 
    GoogleMap gMap = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v =inflater.inflate(R.layout.add_masjid,container,false); 

     gMapView = (MapView) v.findViewById(R.id.map); 
     gMapView.getMapAsync(this); 
     gMapView.onCreate(savedInstanceState); 
     gMapView.onResume(); // needed to get the map to display immediately 
     try { 
      MapsInitializer.initialize(getActivity().getApplicationContext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return v; 
    } 


    @Override 
    public void onMapReady(GoogleMap map) { 
     gMap = map; 
     gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new 
       LatLng(0, 0), 0)); 

     if (gMap != null) { 
      gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 


      gMap.setMyLocationEnabled(); 
     } 
    } 
    @Override 
    public void onResume() { 
     super.onResume(); 
     gMapView.onResume(); 

    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     gMapView.onPause(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     gMapView.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     gMapView.onLowMemory(); 
    } 


} 

add_masjid.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#000000"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignParentStart="true" 
     android:text="@string/xyz" 
     android:textColor="#FFFFFF" 
     android:textSize="20sp" 
     /> 

    <Button 
     android:id="@+id/generalId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/rect1" 
     android:onClick="geoLocate" 
     android:text="@string/abc" 
     android:textColor="#FFFFFF" 
     android:textSize="20sp" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toEndOf="@+id/textView1" 
     android:layout_toStartOf="@+id/generalId" 
     android:ems="10" 
     android:inputType="text" 
     android:background="#FFFFFF" 
     android:labelFor="@+id/editText1" 
     android:layout_above="@+id/map" 
     android:layout_alignParentTop="true" /> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/generalId"> 

    </com.google.android.gms.maps.MapView> 

</RelativeLayout> 
+0

この種の呼び出しを行うためのユーザーの許可があるかどうかは、最初に確認することなく、これを行うことはできません。あなたは、ユーザーがこれを行う許可をアプリに与えたかどうかを明示的に確認する必要があります。 – rmcsharry

+1

はい、私はそれをしました。私の下の答えを見てください。 –

答えて

1

私はそれにいくつかの権限を追加することによって、私の問題を解決しました。

@Override 
    public void onMapReady(GoogleMap map) { 
     gMap = map; 
     gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new 
       LatLng(0, 0), 0)); 

    //add this code... 
     if (gMap != null) { 
      gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
      if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext() 
        , Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext() 
        , Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // here to request the missing permissions, and then overriding 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
       //           int[] grantResults) 
       // to handle the case where the user grants the permission. See the documentation 
       // for ActivityCompat#requestPermissions for more details. 
       return; 
      } 
      gMap.setMyLocationEnabled(true); 
      gMap.getUiSettings().setMyLocationButtonEnabled(true); 


     } 
    } 
関連する問題