开发 WinPhone SDK 开发指南 公交查询

公交查询 最后更新时间: 2021年01月22日

公交站关键字查询

该查询是根据站点关键字查询公交站点,keywords 为必选项 ,同时可以设置城市,详细的参数说明参见参考手册。AMapBusSearch 中提供了三个 BusStopKeyWords 的重载函数, 用户可以根据搜索需求进行选择,示例代码(详见demo中BusStopKeyWords.xaml.cs)如下:

public async void GetBusStopKeyWords(string keywords,string city, uint offset,uint page )  
{  
  //请求查询,keywords为必选项  
  AMapBusStopResults busStoprs = await AMapBusSearch.BusStopKeyWords(keywords, offset, page, city);  
  if (busStoprs.Erro == null && busStoprs.BusStopList != null)  
  {  
    if (busStoprs.BusStopList.Count==0)  
    {  
      MessageBox.Show("无查询结果");  
      return;  
    }  
    busStops = busStoprs.BusStopList;   //公交站点数组  
    int i = 0;  
    foreach (AMapBusStop bs in busStops)   //便利公交站点信息  
    {  
      i++;  
      Debug.WriteLine(bs.Id);  
      Debug.WriteLine(bs.Name);  
      Debug.WriteLine(bs.Location.Lat);  
      Debug.WriteLine(bs.Location.Lon);  
      AMapLocation lc = bs.Location;  
      amap.AddMarker(new AMapMarkerOptions()  
      {  
        Position = new LatLng(lc.Lat, lc.Lon),//amap.Center,//  
        Title = "Title",  
        Snippet = "Snippet",  
        IconUri = new Uri("Images/marker_gps_no_sharing.png", UriKind.Relative),  
      });  
    }  
    Debug.WriteLine(i);  
  }  
  else  
  {  
    MessageBox.Show(busStoprs.Erro.Message);  
  }  
}  

公交线路ID查询

该查询是根据公交线路ID查询公交线路,id为必选项。AMapBusSearch 中提供 BusLineIDSearch 函数,示例代码(详见demo中BusLineIDSearch.xaml.cs)如下:

private async void GetBusLineIDSearch(string id)  
{  
  //请求查询,id为必填项  
  AMapBusLineResults busLines = await AMapBusSearch.BusLineIDSearch(id);  
  if (busLines.Erro==null)    //处理查询结果  
  {  
    if (busLines.BusLineList.Count==0)  
    {  
      MessageBox.Show("无查询结果");  
      return;  
    }  
    List busLine = busLines.BusLineList.ToList();    
    foreach (AMapBusLine bl in busLine)  
    {  
      Debug.WriteLine(bl.Name);  
      List  latlng = latLagsFromString(bl.Polyline);  
      amap.AddPolyline(new AMapPolylineOptions()  
      {  
        Points =latlng ,  
        Color = Color.FromArgb(255, 0, 0, 255),  
        Width = 2,  
      });  
    //起始站  
      amap.AddMarker(new AMapMarkerOptions()  
      {  
        Position = latlng.FirstOrDefault(),  
        Title = "Title",  
        Snippet = "Snippet",  
        IconUri = new Uri("Images/bus_start_pic.png", UriKind.Relative),  
      });  
      //终点站  
      amap.AddMarker(new AMapMarkerOptions()  
      {  
        Position =latlng.LastOrDefault(),  
        Title = "Title",  
        Snippet = "Snippet",  
        IconUri = new Uri("Images/bus_end_pic.png", UriKind.Relative),  
      });  
    }  
    MessageBox.Show(busLine[0].Name);  
  }  
}  

公交线路关键字查询

该查询是根据公交线路关键字查询公交线路,keywords 为必选项 ,同时可以设置城市,详细的参数说明参见参考手册。AMapBusSearch 中提供了三个 BusLineKeyWords 的重载函数,示例代码(详见demo中BusSearchofLine.xaml.cs)如下:

private async void GetBusLineKeyWords(string keywords, string city, uint offset, uint page)  
{  
  //请求查询,keywords为必选项             
  AMapBusLineResults busLines = await AMapBusSearch.BusLineKeyWords(keywords, offset, page,city, Extensions.All);  
  this.Dispatcher.BeginInvoke(() =>  
  {  
    if (busLines.Erro == null && busLines.BusLineList != null)  
    {  
      if (busLines.BusLineList.Count == 0)  
      {  
         MessageBox.Show("无查询结果");  
         return;  
      }  
      IEnumerable bLines = busLines.BusLineList;  
      int i = 0;  
      foreach (AMapBusLine item in busLines.BusLineList)  
      {  
        i++;  
      }  
      Debug.WriteLine("返回结果数:"+i);  
      //显示第一条公交路线  
      //起始站  
      amap.AddMarker(new AMapMarkerOptions()  
      {  
        Position = new LatLng(bLines.FirstOrDefault().Bus_stops[0].Location.Lat, bLines.FirstOrDefault().Bus_stops[0].Location.Lon),  
        Title = "Title",  
        Snippet = "Snippet",  
        IconUri = new Uri("Images/bus_start_pic.png", UriKind.Relative),  
       });  
        //终点站  
      amap.AddMarker(new AMapMarkerOptions()  
      {  
         Position = new LatLng(bLines.FirstOrDefault().Bus_stops.Last().Location.Lat, bLines.FirstOrDefault().Bus_stops.Last().Location.Lon),  
         Title = "Title",  
         Snippet = "Snippet",  
         IconUri = new Uri("Images/bus_end_pic.png", UriKind.Relative),  
      });  
      //公交路线  
      List  lnglats = new List ();  
      lnglats = latLagsFromString(bLines.FirstOrDefault().Polyline );  
      amap.AddPolyline(new AMapPolylineOptions()  
      {  
         Points = lnglats,  
         Color = Color.FromArgb(255, 0, 0, 255),  
         Width = 2,  
      });  
      List  latlngs = new List ();  
       //添加途径公交站  
      int j = 0;  
      foreach (AMapBusStop bs in bLines.FirstOrDefault().Bus_stops)  
      {  
        j++;  
        latlngs.Add(new LatLng(bs.Location.Lat, bs.Location.Lon));  
      }  
      Debug.WriteLine("途径公交站总数" + j);  
      foreach (LatLng  latlng in latlngs)  
      {  
        amap.AddMarker(new AMapMarkerOptions()  
        {  
           Position = latlng,  
           Title = "Title",  
           Snippet = "Snippet",  
           IconUri = new Uri("Images/bx11.png", UriKind.Relative),  
        });  
      }  
    }  
    else  
    {  
        MessageBox.Show(busLines.Erro.Message);  
    }  
  });  
}
返回顶部 示例中心 常见问题 智能客服 公众号
二维码