【发布时间】:2023-03-21 20:02:01
【问题描述】:
我是 Xamarin Android 的新手。我正在尝试在我的应用程序上显示一个基本的谷歌地图。我为谷歌地图 api 设置了 API 密钥等。但是,当我运行模拟器时,会显示一条消息“如果没有 google play 服务,应用将无法运行”。
我已使用 android sdk 管理器安装了所有软件包/SDK,我正在尝试使用 API 级别 22(Android 5.0 Lollipop)。
以下代码用于初始化谷歌地图(从 xamarin 示例下载)。
谁能告诉我我在这里缺少什么。
using System;
using Android.App;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace App2
{
[Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity, IOnMapReadyCallback
{
private GoogleMap _gMap;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
GetMap();
}
private void GetMap()
{
if (_gMap == null)
{
FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
}
}
public void OnMapReady(GoogleMap map)
{
_gMap = map;
}
}
}
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment" />
Android Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App2.App2" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" />
<application android:label="App2" android:icon="@drawable/Icon">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCKBrF-_IrAQlkUyFG7GT4qgEJ7qOgyNRI" />
</application>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
【问题讨论】:
标签: c# android .net google-maps xamarin