2016-10-05 19 views
0

スタート地点に配置されたマーカーの位置を自分の位置に基づいて更新しようとしています。しかし、最初のマーカーは、アプリが開いているときに配置されますが、更新されません。物理的な変更はまったく見られません。「タイマーが呼ばれました」というメッセージがコンソールに書き込まれているため、タイマーが動作していることがわかります。 私の質問:なぜタイマーを通してマーカの位置を更新しないのですか? また、よりよい方法がある場合は、私は提案をすることができます。ここでGPS地図マーカーの位置を更新するXamarin

は、私が持っているコードは次のとおりです。

GoogleMap mMap; 
LocationManager _locationManager; 
Location _currentLocation; 
String _locationProvider; 
TextView addresstxt; 
MarkerOptions options = new MarkerOptions(); 

public void OnMapReady(GoogleMap googleMap)// This works as it should on start up. 
{ 
    mMap = googleMap; 
    LatLng latlng = new LatLng(_currentLocation.Latitude, _currentLocation.Longitude); 
    CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 18); 
    mMap.MoveCamera(camera); 


    options.SetPosition(latlng); 
    options.SetTitle("Vehicle"); 
    options.SetSnippet("Your vehicle is here."); 
    options.Draggable(false); 
    mMap.AddMarker(options); 
} 

private void CountDown() 
{ 

    System.Timers.Timer timer = new System.Timers.Timer(); 
    timer.Interval = 1000; 
    timer.Elapsed += OnTimedEvent; 
    timer.Enabled = true; 


} 

private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e) 
{ 
    Console.WriteLine("Timer called"); 
    mMap.Clear(); 
    LatLng latlng = new LatLng(_currentLocation.Latitude, _currentLocation.Longitude); 
    CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 18); 
    mMap.MoveCamera(camera); 

    MarkerOptions options = new MarkerOptions() 
     .SetPosition(latlng) 
     .SetTitle("Vehicle") 
     .SetSnippet("Your vehicle is here.") 
     .Draggable(false); 

    mMap.AddMarker(options); 
} 

答えて

1

私はあなたがタイマーコールバックが上で実行され、一般的にUIの更新がUIスレッド上で実行する必要があるスレッドかを知ることはできませんので、これはあり得ることを推測しています。 RunOnUiThread()でUIスレッドのマーカーを更新するコードを実行してください。例:

RunOnUiThread(() => 
{ 
    mMap.Clear(); 
    LatLng latlng = new LatLng(_currentLocation.Latitude, _currentLocation.Longitude); 
    CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng, 18); 
    mMap.MoveCamera(camera); 

    MarkerOptions options = new MarkerOptions() 
     .SetPosition(latlng) 
     .SetTitle("Vehicle") 
     .SetSnippet("Your vehicle is here.") 
     .Draggable(false); 

    mMap.AddMarker(options); 
}); 
関連する問題