2016-06-09 3 views
0

Я работаю с Google Maps API для Android. Я использую MapView, так как он находится внутри фрагмента в ViewPager. К сожалению, это не так, как ожидалось. составителя дать мне эту ошибку:MapView не показывает андроид

[doInBackground] Не удалось получить URL: https://play.google.com/store/apps/details?myApp ...

Почему? Я использую это в среде отладки, он должен искать игровой магазин. Вот мой манифест и мои макеты:

<?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, вам необходимо зарегистрировать приложение проекта на консоли разработчика Google и получить ключ API Google, который вы можете добавить к вашему приложению. Тип ключа API, который вам нужен, - это ключ от Android.

Следуйте инструкции здесь, чтобы установить ключ карты API: https://developers.google.com/maps/documentation/android-api/signup#release-cert

  • сделать проект в Google Developers Console
  • Генерировать ключ для использования API Карт
  • Добавить ключ к вашему андроиду манифеста

Просто вызовите метод onCreate() перед вызовом getMapAsync() и после того, как обратный вызов, вам нужно позвонить onResume() на объекте 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; 
} 
} 

Вот демо-приложение демонстрирует, как использовать API Карт для Android с использованием SupportMapFragment - getMapAsync: https://github.com/googlemaps/android-samples/tree/master/ApiDemos