2017-11-03 7 views
0

卿、私は彼のAndroidデバイス上のLocation上のモバイルのcurrent locationたときに、ユーザーのターンを見つける必要があり、その後にその場所をマークVisual Studio 2017Xamarinを使用してアプリケーションを開発マーク・モバイルロケーションXamarin.Android

を使用してGoogle MapMarkerOptionを使用してください。このため私は以下のビデオの案内に従ってステップを踏んだ。https://www.youtube.com/watch?v=rCZN1c2azyE。しかし、地図上に現在の場所がありませんでした。以下は

私のコードです:

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

      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; 

      //LatLng latlng = new LatLng() 
      //MarkerOptions mo = new MarkerOptions(); 
      //mo.SetPosition(new LatLng(Convert.ToDouble(32.73), Convert.ToDouble(74.86))); 
      //mo.SetTitle("Civil Secretarait Jammu"); 
      //googleMap.AddMarker(mo); 

      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) 
     { 
      Double lat, lng; 
      lat = location.Latitude; 
      lng = location.Longitude; 
      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); 

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

答えて

1

あなただけgooMap.MyLocationEnabled = trueを使用することができます。これが機能するには、最初にロケーション許可を確認し、要求する必要があります。

MyLocationEnabledをtrueに設定すると、正確な円とあなたの現在地を示す青い点が表示されます。

+0

これはOnCreate()メソッドまたはOnLocationChanged()メソッドで有効にする必要がありますか? –

+0

ここで 'gooMap'が割り当てられています。おそらく 'OnMapReady()' – Cheesebaron

関連する問題