【问题标题】:Google map doesn't load into my app device谷歌地图无法加载到我的应用设备中
【发布时间】:2017-01-26 14:16:04
【问题描述】:

我是安卓开发的新手。我正在使用互联网和以下教程自学。我正在制作一个具有 3 个tabs 的应用程序,一个用于location,第二个用于camera,第三个用于pictures。为此,我在android studio 内使用tabbed activity。在里面我使用mapView 并遵循tutorial 并完成其中使用的每个步骤。此外,我有我的apiKey 用于谷歌地图。

下面是我的代码

清单

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

Location.java

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;

}

位置布局

<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); 我收到一条红线,上面写着

我尝试进入权限检查但未能成功,因为我是新手,所以我很难找到正确的解决方案

更新 1

更新我的代码后的样子

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

我正在我的安卓设备API 16 上对其进行测试,它给了我build failed 错误“错误:任务':app:transformClassesWithDexForDebug'的执行失败。

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

任何帮助将不胜感激

【问题讨论】:

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


    【解决方案1】:

    对于Android Version &gt;= M,必须在运行时询问权限,这不能自动完成。一般Runtime Permission 询问代码。如果在 Fragment 内部,则使用 getActivity() 而不是 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);
            }
        //}
    }
    

    【讨论】:

    • this 下我有红线
    • 使用getActivity()代替this,因为你在fragment里面。
    • 是的,我知道了,还有一件事我想问一下在哪里放onRequestPermissionsResult
    • 在所有方法之外,但在类内。
    • 对不起,我昨天下车,现在正在实施它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多