首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MapView没有展示安卓

MapView没有展示安卓
EN

Stack Overflow用户
提问于 2016-06-09 14:55:32
回答 2查看 137关注 0票数 0

我正在使用用于Android的Google。我使用的是MapView,因为它在ViewPager中的一个片段中。不幸的是,它并没有像预期的那样显示出来。编译器给出了以下错误:

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

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

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>

代码语言:javascript
运行
复制
<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>

我的活动片段:

代码语言:javascript
运行
复制
<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>

我的片段代码是:

代码语言:javascript
运行
复制
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
    );
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-14 11:40:50

谢谢你的解释,但麻烦在别的地方。当在片段中包含MapView时,必须覆盖所有片段状态(OnResume、OnPause等)并使用MapView。以下是一个示例:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-06-10 16:20:29

要使用Google API,您必须在Google控制台上注册应用程序项目,并获得一个Google密钥,您可以将其添加到应用程序中。您需要的API密钥类型是一个Android密钥。

按照这里的说明设置映射API键:https://developers.google.com/maps/documentation/android-api/signup#release-cert

  • 在Google开发人员控制台中创建一个项目
  • 生成一个键以使用Maps API
  • 将键添加到android清单中

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

代码语言:javascript
运行
复制
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 - getMapAsync:https://github.com/googlemaps/android-samples/tree/master/ApiDemos为Android使用Maps

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37729466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档