谷歌地图不会显示在片段GoogleMap v2 Android中

时间:2015-05-06 07:51:16

标签: android google-maps

我正在开发一个具有活动MapReminderActivity的应用程序,负责在片段id映射中显示地图,但是当渲染活动时,我得到的只是一个空白片段。我的Logcat声明了一些身份验证问题。我检查了Api键甚至重新生成它并尝试使用那个,但仍然是空白片段。

MapReminderActivity:

package com.afifagroup.gps;

import java.io.IOException;
import java.util.List;

import com.afifagroup.gps.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MapReminderActivity extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener{
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;
    GoogleApiClient mLocationClient;
    private Location mCurrentLocation;
    LocationRequest mLocationRequest;
    private static final float DEFAULT_ZOOM = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (servicesOK()) {
            setContentView(R.layout.activity_map_reminder);

            if (initMap()) {
                Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            setContentView(R.layout.activity_main);
        }

    }
    private void gotoLocation(double lat, double lng,
            float zoom) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        mMap.moveCamera(update);
    }
    private void gotoLocation(double lat, double lng) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
        mMap.moveCamera(update);
    }
    public void geoLocate(View v) throws IOException {
        hideSoftKeyboard(v);

        EditText et = (EditText) findViewById(R.id.editText1);
        String location = et.getText().toString();

        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(location, 1);
        Address add = list.get(0);
        String locality = add.getLocality();
        Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

        double lat = add.getLatitude();
        double lng = add.getLongitude();

        gotoLocation(lat, lng, DEFAULT_ZOOM);

    }

    private void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    public boolean servicesOK(){
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(isAvailable == ConnectionResult.SUCCESS){
            return true;
        }
        else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
        else{
            Toast.makeText(this, "Can't connect to Google Play Services", Toast.LENGTH_LONG).show();
        }
        return false;
    }

    private boolean initMap() {
        if (mMap == null) {
            SupportMapFragment mapFrag =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;

    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.d("Connection Falure Error",result.toString());

    }


    @Override
    public void onConnected(Bundle arg0) {
        if(servicesOK()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        }

    }


    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub

    }

}

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.afifagroup.gps"
    android:versionCode="1"
    android:versionName="1.0" 
    android:installLocation="preferExternal">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" 
        android:maxSdkVersion="17"/>
 <permission
        android:name="com.afifagroup.gps.permission.MAPS_RECIEVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.afifagroup.gps.permission.MAPS_RECIEVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.afifagroup.gps.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.afifagroup.gps.AllReminderActivity"
            android:label="@string/title_activity_all_reminder" >
        </activity>
        <activity
            android:name="com.afifagroup.gps.CheckLocationActivity"
            android:label="@string/title_activity_check_location" >
        </activity>
        <activity
            android:name="com.afifagroup.gps.MapPointActivity"
            android:label="@string/title_activity_map_point" >
        </activity>
          <activity
            android:name="com.afifagroup.gps.MapReminderActivity"
            android:label="Select Place" >
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="*****API KEY****************" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

错误日志:

E/dalvikvm(8541): Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
E/dalvikvm(8541): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
E/dalvikvm(8541): [DVM] mmap return base = 469e3000
E/dalvikvm(8541): [DVM] mmap return base = 46f5d000
E/dalvikvm(8541): [DVM] mmap return base = 47b90000

1 个答案:

答案 0 :(得分:0)

尝试从manifest.xml文件中删除以下行

 <uses-permission android:name="com.afifagroup.gps.permission.MAPS_RECIEVE" />

并检查你的layout.xml应该是:

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