问题是我正在尝试获取用户的当前位置并且代码没有给出任何错误,除了代码编译时,它应该执行onLocationChanged方法但它不会执行它。 我还在我的清单文件中设置权限,但我不知道为什么我似乎无法理解问题是什么。请帮忙。
这是我的地图活动,我正在尝试将标记设置为当前位置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Graphics;
using Android.Locations;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;
namespace RoutineApp
{
[Activity(Label = "GoogleMapDirectionsActivity", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, Theme = "@style/Theme.AppCompat.Light.NoActionBar")]
public class GoogleMapDirectionsActivity : Activity, IOnMapReadyCallback, ILocationListener
{
private GoogleMap gMap;
private MapFragment mapFragment;
private LocationManager locationManager;
string locationProvider;
private MarkerOptions markerOptions;
private Marker marker;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.googlemapdirections);
mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.DirectionMap);
mapFragment.GetMapAsync(this);
goBtn = FindViewById<Button>(Resource.Id.GoBtn);
//user location
IntializeLocationManager();
}
private void IntializeLocationManager()
{
locationManager = (LocationManager)GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria
{
Accuracy = Accuracy.Fine
};
IList<string> acceptableLocationProviders = locationManager.GetProviders(criteriaForLocationService, true);
if (acceptableLocationProviders.Any())
{
locationProvider = acceptableLocationProviders.First();
}
else
{
locationProvider = string.Empty;
}
//Log.Debug(TAG, "Using " + locationProvider + ".");
}
public void OnMapReady(GoogleMap googleMap)
{
gMap = googleMap;
//mDirectionMap.TrafficEnabled = true;
gMap.UiSettings.CompassEnabled = true;
gMap.UiSettings.MyLocationButtonEnabled = true;
gMap.UiSettings.ZoomControlsEnabled = true;
OnLocationChanged(locationManager.GetLastKnownLocation(locationProvider));
}
protected override void OnResume()
{
base.OnResume();
locationManager.RequestLocationUpdates(locationProvider, 400, 0, this);
}
protected override void OnPause()
{
base.OnPause();
locationManager.RemoveUpdates(this);
}
public void OnLocationChanged(Location location)
{
if (marker != null)
{
marker.Remove();
}
LatLng latLng = new LatLng(location.Latitude, location.Longitude);
markerOptions = new MarkerOptions()
.SetPosition(latLng)
.SetTitle("My Current Position");
marker = gMap.AddMarker(markerOptions);
CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latLng, 19);
gMap.MoveCamera(camera);
}
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();
}
}
我的地图活动的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:background="@android:color/holo_blue_dark">
<EditText
android:id="@+id/SourceTxtBox"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/edit_text_style"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:hint="Enter Starting Point"
android:layout_weight="7"
android:textColorHint="#a3a3a3"
android:textColor="#000" />
<EditText
android:id="@+id/DestinationTxtBox"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/edit_text_style"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:hint="Enter Destination"
android:layout_weight="7"
android:textColorHint="#a3a3a3"
android:textColor="#000" />
<fragment
android:id="@+id/DirectionMap"
android:layout_height="0dp"
android:layout_width="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_weight="80" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:weightSum="10">
<Button
android:id="@+id/GoBtn"
android:text="Go"
android:textSize="10dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="20dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.company.RoutineApp" android:versionCode="1" android:versionName="1">
<uses-sdk android:minSdkVersion="15" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- 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" />
<!-- Permission to receive remote notifications from Google Play Services -->
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="com.company.RoutineApp.permission.MAPS_RECEIVE" />
<permission android:name="com.company.RoutineApp.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<!-- 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" />
<!--<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>-->
<application android:label="@string/app_name" android:icon="@drawable/Icon" android:theme="@style/Theme.MyTheme">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyAGQlTIquLL5yyIr47pI_AA9g1CI-Whh0o" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>