Getting user location in Xamarin with google play services

Alessandro Canale

I want to show the current user location in an android app like the blue dot in GMaps. I searched online but I'm probably missing something important.

These are the classes or interfaces that my class derives from

public class TargetActivity : AppCompatActivity, IOnMapReadyCallback, Android.Locations.ILocationListener

This code is from my MainActivity, it's my first time programming on Android so I don't really know where to go from here.

    public GoogleMap _map;
    public MapFragment _mapFragment;
    LocationManager locationManager;
    string provider;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Target);

        _mapFragment = FragmentManager.FindFragmentById(Resource.Id.map) as MapFragment;
        if (_mapFragment == null)
        {
            GoogleMapOptions mapOptions = new GoogleMapOptions()
                .InvokeMapType(GoogleMap.MapTypeTerrain)
                .InvokeZoomControlsEnabled(false)
                .InvokeCompassEnabled(true);

            FragmentTransaction fragTx = FragmentManager.BeginTransaction();
            _mapFragment = MapFragment.NewInstance(mapOptions);
            fragTx.Add(Resource.Id.map, _mapFragment, "map");
            fragTx.Commit();


        }
        _mapFragment.GetMapAsync(this);

        if (_map != null)
        {
            _map.UiSettings.ZoomControlsEnabled = false; 
            _map.UiSettings.CompassEnabled = true;  
            _map.MapType = GoogleMap.MapTypeTerrain;   

        }

        locationManager = (LocationManager)GetSystemService(Context.LocationService);
        provider = locationManager.GetBestProvider(new Criteria(), false);

        Location location = locationManager.GetLastKnownLocation(provider);
        if (location == null)
        {
            System.Diagnostics.Debug.WriteLine("no location");
        }
    }

    public void OnMapReady(GoogleMap googleMap)
    {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.SetPosition(new LatLng(16.03, 108));
        markerOptions.SetTitle("AAAA");
        googleMap.AddMarker(markerOptions);
    }

    public void OnLocationChanged(Location location)
    {

        double lat, lng;
        lat = location.Latitude;
        lng = location.Longitude;

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.SetPosition(new LatLng(lat, lng));
        markerOptions.SetTitle("Posizione");
        _map.AddMarker(markerOptions);

        CameraPosition.Builder builder = CameraPosition.InvokeBuilder();
        builder.Target(new LatLng(lat, lng));
        CameraPosition cameraPosition = builder.Build();
        CameraUpdate cameraUpdate = CameraUpdateFactory.NewCameraPosition(cameraPosition);
        _map.MoveCamera(cameraUpdate);

    }

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();
        }
Zannith

You need to implement OnMyLocationButtonClickListener and OnMyLocationClickListener. Enable the location services on your map instance and override the relevant methods. So long as you have permissions to location pretty much everything is taken care of for you. Have a look at the documentation here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Excessive Alarm Manager wakeups in android with Google Play Services Location

Error building Xamarin.Android project with Google Play Services

Google Location Services Vs Android Location Services

Get a user ID from Google Play Services?

Open source replacement for Google Play location services on Android

Is it possible to ask user if he wants to update google play services or not?

I am not getting 'com.google.android.gms:play-services-wallet:8.4.0' update for Google play services in sdk

Android Service With Google Play Services Location Causing App to Open Randomly

Getting Exception with google play services (Google Places API Android) with 10.2.0

How do you prompt the user to update google play services?

Battery drain via Google Play Services persists after location services are used in Android

Getting the last known location with google play services

Getting Google Play services signature invalid

Google Play Services for Android. Location client not update location if wifi disabled

Emulator manual location doesn't work with Google Play Services

Google play services

Contact user using Google Play Game Services

Xamarin Google Play Map Services Error 400 Bad Request

Google Play Services and admob

Accessing Google Play Services Location API in a Service

Is the user required to be logged in to Google Play if I am using Google Play Services?

Can not use Location with newest google play services 7.5.0

Google Play services location API - Counting distance

IllegalAccessError Google Play Services

Altitude from Google Play Services Fused Location Provider

Google Location services Android

Matching Google Play Services device installation with Xamarin.GooglePlayServices libraries

Getting user's location for google Maps

is location detection required google play services android studio