2017-12-18 344 views
1

我正在開發一個應用程序,應該在android或IOS上打開地圖。 我面臨的問題(在Android上,IOS仍未測試)是它打開地圖,總是在相同的位置,這似乎是我的位置,而不是定義的位置。 這裏是我的代碼...爲什麼地圖在同一地點開放? Xamarin.Forms()

public MainPage() 
    { 
     InitializeComponent(); 
     storesList.BackgroundColor = Color.CornflowerBlue; 
     storesList.SeparatorColor=Color.White; 
     storesList.ItemSelected += CellSelected; 
     storesList.ItemTemplate = new DataTemplate(() => 
     { 
      var storeCell = new StoreCell(); 
      storeCell.SetBinding(StoreCell.NameProperty, "Name"); 
      storeCell.SetBinding(StoreCell.LocationProperty, "Location"); 
      storeCell.SetBinding(StoreCell.ScheduleProperty, "Schedule"); 

      return storeCell; 
     }); 

     Stores = new ObservableCollection<Store>(); 

     Store store1 = new Store 
     { 
      Name = "Pombal", 
      Location = new Coordinate(39.9143958, -8.6297282).ToString() 
     }; 
     Store store2 = new Store 
     { 
      Name = "Unknown", 
      Location = new Coordinate(39.7301803, -8.8438668).ToString(), 
      Schedule = "09:00-12:30/13:30-18:00" 
     }; 

     Stores.Add(store1); 
     Stores.Add(store2); 
     Stores.Add(store1); 
     Stores.Add(store2); 
     Stores.Add(store1); 
     Stores.Add(store2); 
     Stores.Add(store1); 
     Stores.Add(store2); 
     storesList.ItemsSource = Stores; 

    } 

    void CellSelected(object sender, SelectedItemChangedEventArgs e) 
    { 
     var address = ((Store)e.SelectedItem).Location.Replace(" ", ""); 
     //storesList.SelectedItem = null; 

     Uri uri; 
     switch (Device.RuntimePlatform) 
     { 
      case Device.iOS: 
       uri = new Uri(string.Format("http://maps.apple.com/?q={0}", address)); 
       System.Diagnostics.Debug.WriteLine(uri); 
       Device.OpenUri(uri); 
      break; 

      case Device.Android: 
       uri = new Uri("geo:" + address); 
       System.Diagnostics.Debug.WriteLine(uri); 
       Device.OpenUri(uri); 
      break; 
     } 
    } 

此外,如果你能幫助我的列表的項目不取消我在哪裏必須把這個storesList.SelectedItem = null;這樣,當我回到我的應用程序,我可以選擇相同的項目作爲我之前(它不工作在那裏我有它,我得到空引用除外)

回答

2

你是不是正確構建自己的地圖網址:

https://www.google.com/maps/search/?api=1&query=47.5951518,-122.3316393 

Docs