如何在Xamarin.android中使用标记在不同位置进行经度和纬度



我能够使用标记来获取当前位置,但无法将标记设置为不同的位置,并使用标记获得了纬度和经度值。

public class googlemapsAc : Activity, ILocationListener, IOnMapReadyCallback
    {
        GoogleMap _map;
        LocationManager locManager;
        TextView loc1, loc2;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
           SetContentView(Resource.Layout.mapsActivity);
            loc1 = FindViewById<TextView>(Resource.Id.btn_loc);
            loc1 = FindViewById<TextView>(Resource.Id.btn_loc2);
            locManager = (LocationManager)GetSystemService(LocationService);
            locManager.RequestLocationUpdates(LocationManager.GpsProvider, 0,0, this);
            Location location = locManager.GetLastKnownLocation(LocationManager.GpsProvider);
            MapFragment mapFrag = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
            mapFrag.GetMapAsync(this);
            if (location != null)
            {
                MarkerOptions options = new MarkerOptions();
                LatLng lat = new LatLng(location.Latitude, location.Longitude);
                 Toast.MakeText(this,"Current location:nLatitude: " + location.Latitude+ "n" + "Longitude: " + location.Longitude,ToastLength.Long).Show();
               options.SetPosition(lat);
               p.AddMarker(options);
            }
            else
            {
                Toast.MakeText(this, "Cannot fetch current location!",ToastLength.Short).Show();
            }
            locManager.RemoveUpdates(this);
        }
        public void OnLocationChanged(Location location)
        {
        }
        public void OnProviderDisabled(string provider)
        {
            throw new NotImplementedException();
        }
        public void OnProviderEnabled(string provider)
        {
            throw new NotImplementedException();
        }
        public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
        {
            throw new NotImplementedException();
        }
        protected  override void OnResume()
        {
            base.OnResume();
            locManager.RequestLocationUpdates(LocationManager.GpsProvider, 0,0, this);
        }
        protected override void OnPause()
        {
            base.OnPause();
            locManager.RemoveUpdates(this); 
        }
        public void OnMapReady(GoogleMap googleMap)
        {
            _map = googleMap;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/btn_loc" />
 <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/btn_loc2" />
    <fragment
        android:id="@+id/map"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

在此代码中,我能够在吐司消息中获取当前位置,但是当我更改标记的位置时,我没有将纬度和经度值置于标记中以将该值绑定到文本视图。

请帮助我使用位置标记绑定到TextView

获得LAT LANG值

在此代码中,我能够在吐司消息中获取当前位置,但是当我更改标记的位置时,我没有将纬度和经度值置于标记中以将该值绑定到文本视图。

我没有看到有关如何更改标记位置的任何代码,但是您可以直接从标记中获得纬度和经度,例如:

var latitude = marker.Position.Latitude;
var longitude = marker.Position.Longitude;

最新更新