片段内的MapView - 标记仅显示在灰色backgorund上

时间:2018-02-09 10:45:18

标签: android google-maps fragment android-mapview

所以我刚开始将谷歌地图添加到我的Android应用程序中。我使用的片段都在我的MainActivity中,所以理想情况下,我也希望在片段中显示mapView。我可以设法显示它,只是添加了自由女神像的随机标记。但是,初始视图仅显示在灰色背景上。当你缩小很多时,可以看到它在美国东海岸的设置,但没有具体细节。 此外,在控制台中我得到以下错误代码,这不会阻止应用程序运行,但我仍然想要追溯它当然

    E/Google Maps Android API: Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
    E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)
                       Ensure that the "Google Maps Android API v2" is enabled.
                       Ensure that the following Android Key exists:
                       API Key: AIzaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxxxx
                       Android Application (<cert_fingerprint>; package_name>): 1:12:12:xxxxxxxxxxxxxxxxx;com.example.myapplication.x

这是我的mapView片段的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Map View"/>

这是java类,它处理mapView:

 package com.example.myapplication.x;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapView;
 import com.google.android.gms.maps.MapsInitializer;
 import com.google.android.gms.maps.OnMapReadyCallback;
 import com.google.android.gms.maps.model.CameraPosition;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;
 import java.util.Map;

public class GetCustomizedOutdoorWorkoutMapView extends Fragment implements OnMapReadyCallback {

MapView mMapView;
GoogleMap mGoogleMap;
View mView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_google_maps, container, false);

    mMapView = (MapView) mView.findViewById(R.id.mapview);
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this); //this is important

    return mView;
}

@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = (MapView) mView.findViewById(R.id.mapview);
    if (mMapView != null) {
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(getContext());

    mGoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mGoogleMap.addMarker(new MarkerOptions().position(/*some location*/ new LatLng(40.689247, -74.044502)).title("Statue Of Liberty").snippet("Not so much liberty in the US rn"));
    CameraPosition Liberty = CameraPosition.builder().target(new LatLng(40.689247, -74.044502)).zoom(16).bearing(0).tilt(45).build();
    mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Liberty));
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

}

我的清单,我添加了以下内容:

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

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSxxxxxxxxxxxxxxxxxxx"/>

任何人都有任何关于我应该注意直接显示地图并解决错误信息的提示?我也尝试过api vs 2而不是.geo.APi,但它的错误代码相同。

1 个答案:

答案 0 :(得分:0)

从错误中可以清楚地看到,您没有为Android启用google map api。要启用,

1 ...登录谷歌开发者控制台 2 ..从左侧面板中选择“库”选项。 3 ...现在,您可以查看所有API列表并转到Google Maps API并选择Google Maps Android API。 4 ...现在您可以看到启用/禁用API的选项。启用它。  它会帮助你。