2014-09-04 97 views
1

我想在iOS Xamarin中獲取用戶的當前位置。這是我的代碼。獲取用戶在Xamarin iOS中的當前位置

CLLocationManager locationManager = new CLLocationManager(); 
locationManager.StartUpdatingLocation(); 
locationManager.StartUpdatingHeading(); 

locationManager.LocationsUpdated += delegate(object sender , CLLocationsUpdatedEventArgs e) 
{ 
    foreach(CLLocation loc in e.Locations) 
    { 
     Console.WriteLine(loc.Coordinate.Latitude); 
    } 
}; 

1.當我啓動我的應用程序,一個對話框「APPNAME想用你的當前位置」持久性有機污染物,並在一兩秒鐘後,自行解散(之前,我可以點擊任何按鈕)。這是爲什麼發生?

2. 當我手動(設置 - >隱私 - >位置)爲我的應用程序啓用位置服務時,應用程序正常工作。我希望這是以編程方式發生的。我已經檢查this post也類似question。我在哪裏錯了?

請幫忙

回答

0

這是用於iOS8嗎?您是否調用了RequestWhenInUseAuthorization?

至於關閉對話框,這很可能是由於locationManager變量未在類級別聲明。

+0

謝謝你的回覆。這不適用於iOS8,移動locationManager到類級別沒有做到這一招。對話框僅在iOS7上解散,在iOS6上正常工作!奇怪! – 2014-09-30 05:59:37