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