我是Xamarin的新手。我正在使用StreetView API进行项目:打开应用程序时,我想查看设备位置的街景视图。
也许正在运行。但是我想编写一个方法,当没有设备位置的街景视图时,返回一个带有“此处没有街景视图”的吐司。
有我的代码:
MainActivity.cs
using Android.App;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android;
using Android.Content.PM;
using Android.Views;
using Android.OS;
using Xamarin.Essentials;
using System;
namespace StreetView
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity, IOnStreetViewPanoramaReadyCallback
{
const int RequestLocationId = 0;
public double latitude;
public double longitude;
readonly string[] LocationPermissions =
{
Manifest.Permission.AccessCoarseLocation,
Manifest.Permission.AccessFineLocation
};
StreetViewPanoramaView streetViewPanoramaView;
StreetViewPanorama streetPanorama;
LatLng latlng;
public void OnStreetViewPanoramaReady(StreetViewPanorama panorama)
{
this.streetPanorama = panorama;
streetPanorama.UserNavigationEnabled = true;
streetPanorama.StreetNamesEnabled = true;
streetPanorama.PanningGesturesEnabled = true;
streetPanorama.ZoomGesturesEnabled = true;
Console.WriteLine("Coordinate: " + latlng);
streetPanorama.SetPosition(latlng);
/*
if (streetPanorama.SetPosition(latlng) == )
{
Console.WriteLine("panorama location: " + panorama);
Toast.MakeText(this, "Position...", ToastLength.Long).Show();
streetPanorama.SetPosition(latlng);
} else
{
Console.WriteLine("panorama location: " + panorama);
Toast.MakeText(this, "There isn't StreetView here", ToastLength.Long).Show();
}*/
}
public override void OnWindowFocusChanged(bool hasFocus)
{
base.OnWindowFocusChanged(hasFocus);
if (hasFocus && (int)Build.VERSION.SdkInt >= 19)
{
Window.DecorView.SystemUiVisibility =
(StatusBarVisibility)(SystemUiFlags.LayoutStable
| SystemUiFlags.HideNavigation
| SystemUiFlags.LayoutFullscreen
| SystemUiFlags.Fullscreen
| SystemUiFlags.ImmersiveSticky);
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
if (requestCode == RequestLocationId)
{
if ((grantResults.Length == 1) && (grantResults[0] == (int)Permission.Granted))
{
// Permissions granted - display a message.
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
async System.Threading.Tasks.Task GetPositionAsync()
{
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
latitude = location.Latitude;
longitude = location.Longitude;
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
}
}
protected override void OnCreate(Bundle bundle)
{
GetPositionAsync();
Console.WriteLine("Latitudine" + latitude + "Longitudine" + longitude);
if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
{
RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
}
base.OnCreate(bundle);
SetContentView(Resource.Layout.activity_main);
var lat = Intent.GetDoubleExtra("ltd", latitude);
var lng = Intent.GetDoubleExtra("lng", longitude);
//create LatLng to display
latlng = new LatLng(lat, lng);
//find our panorama view and create async.
streetViewPanoramaView = FindViewById<StreetViewPanoramaView>(Resource.Id.panorama);
streetViewPanoramaView.OnCreate(bundle);
streetViewPanoramaView.GetStreetViewPanoramaAsync(this);
}
}
}
在Android中,这是方法:(我的梦想)
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
mPanorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
@Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
if (streetViewPanoramaLocation != null && streetViewPanoramaLocation.links != null) {
// location is present
} else {
// location not available
}
}
});
任何人都可以帮助我在Xamarin和我的项目中编写此方法吗?
非常感谢!
在C#中,您的Java方法如下所示。将其添加到您的OnStreetViewPanoramaReady()
方法:
panorama.StreetViewPanoramaChange += (sender, args) =>
{
if (args.Location != null && args.Location.Links != null)
{
// location is present
}
else
{
// location not available
}
};
通常,在Xamarin世界中,使用C#事件代替Java侦听器模式。当然,通过使用SetOnStreetViewPanoramaChangeListener()
对象的panorama
方法并实现StreetViewPanorama.IOnStreetViewPanoramaChangeListener
接口,您也可以采用侦听器的方式。由于当前在C#中,您无法像在Java中那样使用匿名类型实现接口,因此使用事件仍然是最优雅,最合理的解决方案。