Google地图无法加载到我的应用设备中

时间:2017-01-26 14:16:04

标签: android google-maps android-fragments android-tabactivity

我是android开发的新手。我正在学习自己使用互联网和以下教程。我正在创建一个包含3 location的应用,其中一个用于camera秒,用于pictures,第三个用于tabbed activity。为此,我在android studio内使用mapView。在其中我使用apiKey并遵循此tutorial并完成其中使用的每个步骤。此外,我有<uses-permission android:name="com.example.accurat.faisal.permission.MAPS_RECEIVE"/> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <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.geo.API_KEY" android:value="AIzaSyBhhxKOLbX2I3kNbsZy8lbSXtjuAijKL94" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 谷歌地图。

以下是我的代码

清单

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflat and return the layout
    View rootView = inflater.inflate(R.layout.my_location, container, false);

    mMapView = (MapView)rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();

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

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


            googleMap.setMyLocationEnabled(true);

            LatLng loc = new LatLng(31.492370,74.329060);

            googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));

            // For zooming automatically to the location of the marker
            CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


        }
    });


    return rootView;

}

Location.java

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<com.google.android.gms.maps.MapView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mapView" />

位置布局

googleMap.setMyLocationEnabled(true);

此外,在 mMapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION },1); } else { googleMap.setMyLocationEnabled(true); LatLng loc = new LatLng(31.492370,74.329060); googleMap.addMarker(new MarkerOptions().position(loc).title("I am Here")); // For zooming automatically to the location of the marker CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } } } }); @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { } else { // permission denied, boo! Disable the // functionality that depends on this permission. //finish(); } return; } // other 'case' lines to check for other // permissions this app might request } } ,我得到一条红线,上面写着

enter image description here

我试图进入许可检查,但无法成功,因为我是新手,所以我在找到正确的解决方案时遇到困难

更新1

更新我的代码后看起来像

API 16

我正在我的Android设备build failed上对其进行测试,它给我Programmes错误“错误:任务执行失败&#39;:app:transformClassesWithDexForDebug&#39;。

  

com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.dex.DexIndexOverflowException:方法ID不在[0, 0xffff]:65536`

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

对于Android Version >= M,必须在运行时询问权限,这不能自动完成。一般Runtime Permission询问代码。如果在getActivity()内而不是Fragment,请使用this

    @Override
    public void onMapReady(GoogleMap googleMap) {


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                &&
                ActivityCompat.checkSelfPermission
                        (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 1);
            //return;
        }
     else{
        googleMap.setMyLocationEnabled(true);

        LatLng loc = new LatLng(31.492370,74.329060);

        googleMap.addMarker(new MarkerOptions().position(loc).title("I am here"));

        // For zooming automatically to the location of the marker
        CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
      }
    }

处理许可:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            case 1:
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    // permission not granted
                }
                else {
                    // permission granted
                }
                break;
            //default:
                //super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    //}
}