MapView没有显示android

时间:2016-06-09 14:55:32

标签: android google-maps android-fragments

我正在使用Google Maps API For Android。 我正在使用MapView,因为它位于ViewPager中的Fragment中。 不幸的是,它没有按预期显示。 编译器给我这个错误:

  

[doInBackground]无法检索网址:https://play.google.com/store/apps/details?myApp ...

为什么?我在调试环境中使用它,它应该寻找playstore。 这是我的清单和布局:

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

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

<permission
    android:name="faurecia.captordisplayer.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="faurecia.captordisplayer.permission.MAPS_RECEIVE" />
<!-- Permission pour utiliser la connexion internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission permettant de vérifier l'état de la connexion -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission pour stocker des données en cache de la map -->
<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"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name=".Application">

    <uses-library android:name="com.google.android.maps" />

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

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的活动片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@android:color/black">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="300px"
        android:layout_height="300px"
        android:apiKey="AIzaSyCKYeYG6IDbaRAs-rLmS3W_Zx8q742F5VU"/>

    <faurecia.captordisplayer.view.GradientGauge
        android:id="@+id/gradientGauge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <faurecia.captordisplayer.view.EnginePowerGauge
        android:id="@+id/engineGauge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <faurecia.captordisplayer.view.Speedmeter
        android:id="@+id/speedmeter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <faurecia.captordisplayer.view.RankingGauge
        android:id="@+id/rankingGauge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

和我的片段代码:

public class HMI0Fragment extends HMIFragment implements OnMapReadyCallback {
@Bind(R.id.mapview) MapView mMapView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    reScheduleTimerTask(TIMER_PERIOD);
    mView = inflater.inflate(R.layout.fragment_hmi0, container, false);
    ButterKnife.bind(this, mView);
    mMapView.getMapAsync(this);
    return mView;
}

@Override
public void onMapReady(GoogleMap map) {
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(-18.142, 178.431), 2));

    // Polylines are useful for marking paths and routes on the map.
    map.addPolyline(new PolylineOptions().geodesic(true)
            .add(new LatLng(-33.866, 151.195))  // Sydney
            .add(new LatLng(-18.142, 178.431))  // Fiji
            .add(new LatLng(21.291, -157.821))  // Hawaii
            .add(new LatLng(37.423, -122.091))  // Mountain View
    );
}

}

2 个答案:

答案 0 :(得分:0)

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

按照此处的说明设置地图API密钥:https://developers.google.com/maps/documentation/android-api/signup#release-cert

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

在调用onCreate()之前调用getMapAsync()方法,调用回调后,需要在MapView对象上调用onResume()

public class MainActivity extends MapActivity {

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

if (getView() != null) {

final MapView mapView = (MapView)getView().findViewById(R.id.mapView);

mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {

@Override
public void onMapReady(GoogleMap googleMap) {
LatLng coordinates = new LatLng(match.match.LocationLatitude, match.match.LocationLongitude);
googleMap.addMarker(new MarkerOptions().position(coordinates).title(match.match.LocationAddress));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 15));
mapView.onResume();
}
}
}
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

这是一个演示应用,演示如何使用SupportMapFragment使用Maps API for Android - getMapAsync:https://github.com/googlemaps/android-samples/tree/master/ApiDemos

答案 1 :(得分:0)

感谢您的解释,但麻烦在其他地方。 在Fragment中包含MapView时,必须覆盖所有片段状态(OnResume,OnPause等)并使用MapView。 这是一个示例:

public class HMI0Fragment extends HMIFragment implements OnMapReadyCallback {
private static int TIMER_PERIOD = 4000;
private static String NAME = "HMI0";

@Bind(R.id.mapview) MapView mMapView;

private boolean mapsSupported = true;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    MapsInitializer.initialize(getActivity());

    if (mMapView != null) {
        mMapView.onCreate(savedInstanceState);
    }
    initializeMap();
}

private void initializeMap() {
    if ( mapsSupported) {
        mMapView = (MapView) getActivity().findViewById(R.id.mapview);
        mMapView.getMapAsync(this);
        //setup markers etc...
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    reScheduleTimerTask(TIMER_PERIOD);
    mView = inflater.inflate(R.layout.fragment_hmi0, container, false);
    ButterKnife.bind(this, mView);
    return mView;
}

@Override
public void onMapReady(GoogleMap map) {
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(-18.142, 178.431), 2));

    // Polylines are useful for marking paths and routes on the map.
    map.addPolyline(new PolylineOptions().geodesic(true)
            .add(new LatLng(-33.866, 151.195))  // Sydney
            .add(new LatLng(-18.142, 178.431))  // Fiji
            .add(new LatLng(21.291, -157.821))  // Hawaii
            .add(new LatLng(37.423, -122.091))  // Mountain View
    );
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mMapView.onSaveInstanceState(outState);
}

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

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

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

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

}