2014-03-03 1 views
0

위치 모드의 상태를 확인하기 위해 다음 코드를 시도했지만 나에게 적합하지 않습니다.위치를 아는 방법은 Windows phone 8에서 켜기/끄기입니까?

Geolocator locationservice = new Geolocator(); 
if (locationservice.LocationStatus == PositionStatus.Disabled) 
{ 
locationButton.Opacity = 0.5; 
} 
+0

'작동하지 않는다'는 것은 무엇입니까? –

+0

@igral OnNavigatedTo 메서드에서 위의 코드를 붙여 넣었습니다. 페이지가로드 될 때 위치가 활성화되어 있지 않다는 메시지가 항상 나타납니다. – JIKKU

답변

1

이 코드를 사용해보십시오. 위치 서비스에서 데이터를 가져오고 위치 서비스 상태가 변경되면 변경 알림을 발생시킵니다. 이 코드의 경우 msdn

using System.Device.Location; 

// Click the event handler for the “Start Location” button. 
private void startLocationButton_Click(object sender, RoutedEventArgs e) 
{ 
    // The watcher variable was previously declared as type GeoCoordinateWatcher. 
    if (watcher == null) 
    { 
     watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy 
     watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal 
     watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged); 
     watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); 
    } 
    watcher.Start(); 
} 

void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) 
{ 
    switch (e.Status) 
    { 
     case GeoPositionStatus.Disabled: 
      // The Location Service is disabled or unsupported. 
      // Check to see whether the user has disabled the Location Service. 
      if (watcher.Permission == GeoPositionPermission.Denied) 
      { 
       // The user has disabled the Location Service on their device. 
       statusTextBlock.Text = "you have this application access to location."; 
      } 
      else 
      { 
       statusTextBlock.Text = "location is not functioning on this device"; 
      } 
      break; 

     case GeoPositionStatus.Initializing: 
      // The Location Service is initializing. 
      // Disable the Start Location button. 
      startLocationButton.IsEnabled = false; 
      break; 

     case GeoPositionStatus.NoData: 
      // The Location Service is working, but it cannot get location data. 
      // Alert the user and enable the Stop Location button. 
      statusTextBlock.Text = "location data is not available."; 
      stopLocationButton.IsEnabled = true; 
      break; 

     case GeoPositionStatus.Ready: 
      // The Location Service is working and is receiving location data. 
      // Show the current position and enable the Stop Location button. 
      statusTextBlock.Text = "location data is available."; 
      stopLocationButton.IsEnabled = true; 
      break; 
    } 
} 
관련 문제