无法设置Google地图v2

时间:2016-02-15 05:50:59

标签: android google-maps

我正在尝试一周时间来设置我的Google Map v2。但它不适合我。我尝试了与之相关的问题的答案,但没有用。我无法追踪确切的问题。请帮忙。 这是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<fragment 
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

在我的map.class中我有

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.*;
import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import android.os.Bundle;

public class Map extends Activity {

    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        if (googleMap == null) {
            googleMap = ((MapFragment)     getFragmentManager().findFragmentById(
                R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
            }
        }

    }
    }

我的API密钥是正确的,我根据指示导入了google play服务库。在我的清单文件中

<permission
        android:name="com.example.sellatease.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.sellatease.permission.MAPS_RECEIVE" />

       <uses-sdk
        android:minSdkVersion="21"
        android:targetSdkVersion="21" />

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

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />

This is my error log

在控制台中我找不到googleplayserviceslib.apk错误。

new error log

3 个答案:

答案 0 :(得分:2)

如果您使用 SupportMapFragment ,则需要扩展 FragmentActivity / AppCompatActivity

  

public class Map扩展了FragmentActivity / AppCompatActivity {

   FragmentManager myFragmentManager = getSupportFragmentManager();
   SupportMapFragment mySupportMapFragment  = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
   googleMap = mySupportMapFragment.getMap();

您可以查看 Android Map V2

答案 1 :(得分:0)

试试这个

googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

答案 2 :(得分:0)

试试这个......

<强> activity_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
    <fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

<强> Map.java

public class Map extends android.support.v4.app.FragmentActivity {

    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        setUpMapIfNeeded();
    }
    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

  private void setUpMapIfNeeded() {

     if (mMap == null) {

       mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();

       if (mMap != null) {
         setUpMap();
       }
     }
  }

<强>的AndroidManifest.xml

 ....
<permission
    android:name="com.example.sellatease.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-sdk
    android:minSdkVersion="21"
    android:targetSdkVersion="21" />

<uses-permission 
    android:name="com.example.sellatease.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >    

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />


         <activity
            android:name=".Map"
            android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>

   </application>
   ....

<强>更新

以编程方式创建Google地图。

<强> activity_map.xml

    <FrameLayout
        android:gravity="top"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/mapContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

<强> Map.java

public class Map extends AppCompatActivity{

private GoogleMap mMap;
private SupportMapFragment mMapFragment;

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

        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction =  getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.mapContainer, mMapFragment, "map");
        fragmentTransaction.commit();

        mMapFragment.getMapAsync(mapReadyCallback);
   }

    private OnMapReadyCallback mapReadyCallback = new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.setMyLocationEnabled(true);
        mMap.setTrafficEnabled(true);
        mMap.setIndoorEnabled(true);
        mMap.setBuildingsEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
    }
};