Google地图Xamarin.Android上的用户当前位置正在更改

时间:2017-11-06 11:27:47

标签: xamarin xamarin.android

先生,在我的应用程序中,我正在获取用户位置(blue dot上的google map),但它会在一秒后更改,或者我点击地图上的GPS图标。

不知道我做错了什么。因此,请提出有关此问题的建议。

活动代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Gms.Maps;
using Android.Locations;
using Android.Util;
using Android.Gms.Maps.Model;

namespace smvdappdev
{
    [Activity(Label = "LOCATION MAP")]
                                                 //for google map,    for gps location
    public class UserLocationMap_Act : Activity, IOnMapReadyCallback, ILocationListener
    {
        //Map variable
        private GoogleMap gooMap;
        //Location
        LocationManager locManager;
        String provider;

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

            // Create your application here
            SetContentView(Resource.Layout.UserLocationMap);

            //Back Button
            ActionBar.SetDisplayHomeAsUpEnabled(true);

            //Method for Map
            SetUpMap();

            //gooMap.MyLocationEnabled = true;
            locManager = (LocationManager)GetSystemService(Context.LocationService);

            provider = locManager.GetBestProvider(new Criteria(), false);

            Location location = locManager.GetLastKnownLocation(provider);
            if (location == null)
            {
                System.Diagnostics.Debug.WriteLine("No location available!");
            }
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Android.Resource.Id.Home:
                    Finish();
                    return true;

                default:
                    return base.OnOptionsItemSelected(item);
            }
        }

        //for setting map on fragment placed on activity page
        private void SetUpMap()
        {
            if (gooMap == null)
            {
                FragmentManager.FindFragmentById<MapFragment>(Resource.Id.fragment1).GetMapAsync(this);
            }
        }

        //to draw map on map display view
        public void OnMapReady(GoogleMap googleMap)
        {
            this.gooMap = googleMap;
            gooMap.MyLocationEnabled = true;

            googleMap.UiSettings.CompassEnabled = true;
            googleMap.UiSettings.ZoomControlsEnabled = true;
            googleMap.MoveCamera(CameraUpdateFactory.ZoomIn());
            //throw new NotImplementedException();
        }

        //*** Here all code for getting location via GPS
        protected override void OnResume()
        {
            base.OnResume();
            provider = LocationManager.GpsProvider;

            //if (locManager.IsProviderEnabled(provider))
            //{
                locManager.RequestLocationUpdates(provider, 2000, 1, this);
            //}
            //else
            //{
            //    Log.Info(tag, provider + " is not available. Does the device have location services enabled?");
            //}
        }

        protected override void OnPause()
        {
            base.OnPause();
            locManager.RemoveUpdates(this);
        }

        public void OnProviderEnabled(string provider)
        {
        }

        public void OnProviderDisabled(string provider)
        {
        }

        public void OnStatusChanged(string provider, Availability status, Bundle extras)
        {
        }

        public void OnLocationChanged(Location location)
        {
            //gooMap.MyLocationEnabled = true;

            Double lat, lng;
            lat = location.Latitude;
            lng = location.Longitude;
            //TextView txtv = FindViewById<TextView>(Resource.Id.txt1);
            //txtv. = "#000000";
            //txtv.Text = "Lattitude : " + lat.ToString() + ", Long : " + lng.ToString();
            MarkerOptions mo = new MarkerOptions();
            mo.SetPosition(new LatLng(lat, lng));
            //Toast.MakeText(this, "Latitude:" + lat.ToString() + ", Longitude:" + lng.ToString(), ToastLength.Long).Show();
            mo.SetTitle("You are here!");
            gooMap.AddMarker(mo);

            //LatLng latlng = new LatLng(Convert.ToDouble(27.50801), Convert.ToDouble(82.02707));

            //Move Camera
            CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
            builder.Target(new LatLng(lat, lng));
            CameraPosition camPos = builder.Build();
            CameraUpdate camUpdate = CameraUpdateFactory.NewCameraPosition(camPos);
            gooMap.MoveCamera(camUpdate);
        }
    }
}

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smvdappdev.smvdappdev" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="16" />
  <application android:label="smvdappdev">
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBjeg79HAm8MBNisbQbVHvtWWaV0IuOWMA" />
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  </application>

  <user-permission android:name="com.smvdappdev.smvdappdev.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
  <uses-permission android:name="com.smvdappdev.smvdappdev.permission.MAPS_RECEIVE" />
  <uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<!-- 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" />

</manifest>

1 个答案:

答案 0 :(得分:1)

将您的网络配置表单Wifi更改为Mobile data,以解决此问题。