1

我正在尝试在 Xamarin for Android 中使用 Google Map API,到目前为止,它是一种令人毛骨悚然的体验。我是 Xamarin 的新手,并试图掌握它。我已经测试了一些基本控件,它们似乎工作正常。现在我正在尝试使用 Google Map Api。我已根据 Xamarin 文档在 AndroidManifest.xml 中插入了 Api 密钥。我的 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"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

这是我的活动课

[Activity (Label = "Testing", MainLauncher = true)]
    public class MainActivity : Android.GoogleMaps.MapActivity
    {

        protected override bool IsRouteDisplayed {
            get {
                return false;
            }
        }

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);


            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            //Button button = FindViewById<Button> (Resource.Id.myButton);

            //button.Click += delegate {
            //    button.Text = string.Format ("{0} clicks!", count++);
            //};
        }
    }

当我运行上面的代码时,它会引发异常

Android.Views.InflateException: Loading

我没有像我们通常在 VS 中那样得到错误的任何细节。我不知道出了什么问题我现在只是盲目地遵循文档以在屏幕上看到一些东西然后玩它,但我猜这不会很快发生!

我为此测试项目的目标是 Google API 版本 8。

请建议我在这里缺少什么?

4

2 回答 2

0

这不完全是您问题的答案,而是另一种选择;

我一直在使用 OpenStreetMaps 与传单和/或使用xamarin over android 的 mapbox。虽然我不确定你是否可以过滤掉加油站。

这是我用来渲染移动地图的代码;

public class MainActivity : Activity//, ILocationListener
{
    private WebView _webView;
    private MonkeyWebViewClient _webViewClient;
    private MonkeyWebChromeClient _webChromeClient;

    ...

    protected override void OnCreate (Bundle bundle)
    {
        Log.Verbose (LogAppTag, "Init webview..");
        _webView = FindViewById<WebView> (Resource.Id.webView1);
        _webView.Settings.JavaScriptEnabled = true;

        _webViewClient = new MonkeyWebViewClient (this);
        _webChromeClient = new MonkeyWebChromeClient ();
        _webViewClient.OnLoadComplete += WebLoadComplete;

        _webView.SetWebViewClient(_webViewClient);
        _webView.SetWebChromeClient(_webChromeClient);      

        _webView.LoadUrl("file:///android_asset/Content/Map/Map.html");

    ...

    void WebLoadComplete (object sender, EventArgs e)
    {
        Log.Verbose(Logging.AppTag, "WebLoadComplete()");
        _webView.LoadUrl("javascript:SetLocation(" + _location.Latitude + "," + _location.Longitude + ");");    
    }
    ...
    class MonkeyWebChromeClient : WebChromeClient {
        public override bool OnJsAlert(WebView view, string url, string message, JsResult result)
        {
            // the built-in alert is pretty ugly, you could do something different here if you wanted to
            return base.OnJsAlert(view, url, message, result);
        }
    }

    class MonkeyWebViewClient : WebViewClient 
    {
        public bool LoadingFinished { get; private set; }

        public delegate void LoadCompleteHandler(object sender, EventArgs e);
        public event LoadCompleteHandler OnLoadComplete;

        Activity context;

        public MonkeyWebViewClient(Activity context)
        {
            this.context = context;
        }
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            view.LoadUrl (url);
            return true;
        }

        public override void OnPageStarted (WebView view, string url, Android.Graphics.Bitmap favicon)
        {
            this.LoadingFinished = false;
            base.OnPageStarted (view, url, favicon);
        }

        public override void OnPageFinished (WebView view, string url)
        {
            this.LoadingFinished = true;

            var handler = OnLoadComplete;
            if (handler != null) handler(this, EventArgs.Empty);

            base.OnPageFinished (view, url);
        }
    }

这是我们在 webview 中加载的 html 代码;

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />   
<!-- mapbox -->
<script src='http://api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.js'></script>
<link href='http://api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.css' rel='stylesheet' />    
<!-- mapbox -->
    </head>
<style>
    body {
        padding: 0;
        margin: 0;
    }
    html, body, #map {
        height: 100%;
    }
</style>
<div id="map"></div>        
<script src="JS/Map.js"></script>           
 </html>

以及可以与C#代码通信的JS代码;您可能想在 mapbox 上创建一个新地图并更改 raistlinthewiz.map-oukwofv3 部分。

    // map code.            
function SetLocation(lat, lng)
{
    var latlng = new L.LatLng(parseFloat(lat), parseFloat(lng));

    // mapbox
    var map = L.mapbox.map('map', 'raistlinthewiz.map-oukwofv3').setView(latlng, 18);                   

    L.mapbox.markerLayer({ // this feature is in the GeoJSON format: see geojson.org for the full specification
        type: 'Feature',
        geometry: {
            type: 'Point', // coordinates here are in longitude, latitude order because x, y is the standard for GeoJSON and many formats
            coordinates: [parseFloat(lng), parseFloat(lat)]
        },
        properties: { // one can customize markers by adding simplestyle properties http://mapbox.com/developers/simplestyle/
            title: 'A Single Marker',
            description: 'Just one of me',              
            'marker-size': 'large'
        }
    }).addTo(map);
}

如果您还需要,我可以提供一个使用 vanilla-leaflet 的示例。

于 2013-10-03T09:47:13.130 回答
0

如果您使用的是 Google Maps V2,请查看此页面: http ://components.xamarin.com/view/googleplayservices/

如果您的目标是 API 8,请不要忘记引用 Mono.Android.Support.v4 程序集,并相应地使用元素!

这是我的布局的样子:

<FrameLayout
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:longClickable="true"
        android:layout_below="@+id/textLayout"
        android:layout_above="@+id/footerLayout" />

我的班级:

public class GoogleMapActivity : Android.Support.V4.App.FragmentActivity
{
      protected override void OnCreate(Bundle bundle)
      {
        var fragTx = SupportFragmentManager.BeginTransaction();
        var mapFragment = Android.Gms.Maps.SupportMapFragment.NewInstance(mapOptions);
        fragTx.Add(Resource.Id.mapView, mapFragment, "mapView");
        fragTx.Commit();
      }
 }
于 2013-10-02T14:26:56.710 回答