Google Maps Android API v2不显示地图

时间:2015-08-07 17:53:54

标签: android google-maps

使用Google Map Android API v2显示地图时遇到问题。

app debug不显示任何错误,但是当我调用Activity时,我得到一个没有地图的空白MapView。

我的代码是:

activity_map.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
>

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

</LinearLayout> 

MapViewerActivity

public class MapViewerActivity extends Activity {

    MapView mapView;
    GoogleMap map;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_map);

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);

    MapsInitializer.initialize(this);

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);
}

有什么想法吗? Tyvm阅读!

2 个答案:

答案 0 :(得分:1)

来自Google的文档:

  

要使用Google Maps Android API v2,您必须在Google Developers Console上注册您的应用项目,并获取可以添加到您应用的Google API密钥。您需要的API密钥类型是Android密钥。

按照Google的说明设置地图API密钥

https://developers.google.com/maps/documentation/android/signup

简而言之:

  • 在Google Developers Console中制作项目
  • 生成使用Maps API的密钥
  • 将密钥添加到您的Android清单

答案 1 :(得分:0)

试试这种方式,

activity_map

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapViewerActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

MapViewerActivity

public class MapViewerActivity extends Activity implements OnMapReadyCallback {

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

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }
}

最后确保您添加了以下功能&amp;清单中的权限,

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

  <!-- Creating Permission to receive Google Maps -->
    <permission
        android:name="com.example.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <!-- Permission to receive Google Maps -->
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />

     <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="@string/google_maps_key" />

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

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

确保使用正确的API密钥,并在API控制台中启用了Google Maps v2。

相关问题