2016-06-09 68 views
0

我正在使用Google Maps API For Android。 我使用MapView,因为它位于ViewPager中的片段内部。 不幸的是,它没有按预期显示。 的compilator给我这个错误:MapView未显示android

[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 
    ); 
} 

}

回答

0

感谢您的解释,但麻烦的是在其他地方。 当在片段中包含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(); 
} 

}

0

要使用Google Maps Android API,你必须注册在谷歌开发者控制台您的应用项目,并让您可以添加到您的应用程序谷歌的API密钥。您需要的API密钥类型是Android密钥。

按照这里的指令设置一个地图API密钥:https://developers.google.com/maps/documentation/android-api/signup#release-cert

  • 做一个项目在谷歌开发者控制台
  • 生成密钥使用Maps API
  • 关键添加到你的Android清单

只要打电话给onCreate()方法调用getMapAsync(),一旦回调被调用时,您需要致电前MapView对象。

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使用地图API为Android - getMapAsync:https://github.com/googlemaps/android-samples/tree/master/ApiDemos