【发布时间】:2021-06-09 01:42:42
【问题描述】:
在我的地图应用程序中,当我使用除坐标(纬度和经度)以外的所有位置数据填充表格时,我不希望用户知道/提供),这些值稍后会以编程方式确定数据所代表的地图首次加载到应用中时。
当我从主窗体加载现有地图时,代码是这样的:
private void loadExistingMapToolStripMenuItem_Click(object sender, EventArgs e)
{
bool pushpinsAdded = false;
RemoveCurrentPushpins();
using (var frmLoadExistingMap = new mdlDlgFrm_LoadExistingMap())
{
if (frmLoadExistingMap.ShowDialog() == DialogResult.OK)
{
foreach (Pushpin pin in frmLoadExistingMap.Pins)
{
this.userControl11.myMap.Children.Add(pin);
pushpinsAdded = true;
}
}
}
if (pushpinsAdded)
{
RightsizeZoomLevelForAllPushpins();
}
lblMapName.Text = currentMap;
lblMapName.Visible = true;
}
“加载现有地图”表单 (frmLoadExistingMap) 中的代码确定地址的坐标。如果坐标已经存在,那就太好了;否则,将进行 REST 调用以获取这些值:
private async void AddPushpinToListOfPushpins(string location, string fullAddress, string mapDetailNotes, double latitude, double longitude, string pushpinColor)
{
iContentCounter = iContentCounter + 1;
string toolTip = string.Empty;
// if already have the record, including the coordinates, no need to make the REST call
if ((latitude != 0.0) && (longitude != 0.0))
{
if (mapDetailNotesExist(location))
{
toolTip = String.Format("{0}{1}{2}{1}{3},{4}{1}{5}", location, Environment.NewLine, fullAddress, latitude, longitude, mapDetailNotes.Trim());
}
else
{
toolTip = String.Format("{0}{1}{2}{1}{3},{4}", location, Environment.NewLine, fullAddress, latitude, longitude);
}
var _mapLocation = new Microsoft.Maps.MapControl.WPF.Location(latitude, longitude);
this.Pins.Add(new Pushpin()
{
Location = _mapLocation,
ToolTip = toolTip,
Content = iContentCounter,
Background = new SolidColorBrush(GetColorForDesc(pushpinColor))
});
}
else
{
// from https://stackoverflow.com/questions/65752688/how-can-i-retrieve-latitude-and-longitude-of-a-postal-address-using-bing-maps
var request = new GeocodeRequest();
request.BingMapsKey = "heavens2MurgatroidAndSufferinSuccotash";
request.Query = fullAddress;
var result = await request.Execute();
if (result.StatusCode == 200)
{
var toolkitLocation = (result?.ResourceSets?.FirstOrDefault())
?.Resources?.FirstOrDefault()
as BingMapsRESTToolkit.Location;
var _latitude = toolkitLocation.Point.Coordinates[0];
var _longitude = toolkitLocation.Point.Coordinates[1];
var mapLocation = new Microsoft.Maps.MapControl.WPF.Location(_latitude, _longitude);
this.Pins.Add(new Pushpin()
{
Location = mapLocation,
ToolTip = String.Format("{0}{1}{2}{1}{3},{4}{1}{5}", location, Environment.NewLine, fullAddress, _latitude, _longitude, mapDetailNotes),
Content = iContentCounter,
Background = new SolidColorBrush(GetColorForDesc(pushpinColor))
});
UpdateLocationWithCoordinates(location, _latitude, _longitude);
}
}
}
当到达上面代码中的“else”块时(当坐标在数据库中尚不存在时),第一次不会添加图钉。但是,如果我立即再次运行相同的代码,它就可以工作 - 即使不停止并重新启动应用程序。
我知道问题与异步代码有关,但为什么它从来没有(到目前为止)第一次工作,但总是(到目前为止)第二次工作?
更重要的是,有没有一种方法可以让我第一次使用它?
更新
对于 Reza Aghaei:
private void UpdateLocationWithCoordinates(string location, double latitude, double longitude)
{
String query = "UPDATE CartographerDetail " +
"SET Latitude = @Latitude, " +
"Longitude = @Longitude " +
"WHERE FKMapName = @FKMapName AND LocationName = @LocationName";
try
{
var con = new SqliteConnection(connStr);
con.Open();
SqliteCommand cmd = new SqliteCommand(query, con);
// FKMapName and LocationName are for the WHERE clause
cmd.Parameters.AddWithValue("@FKMapName", mapName);
cmd.Parameters.AddWithValue("@LocationName", location);
// Updated values
cmd.Parameters.AddWithValue("@Latitude", latitude);
cmd.Parameters.AddWithValue("@Longitude", longitude);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是“加载现有地图”表单:
它的“加载选定地图”按钮点击这样做:
private void btnLoadSelectedMap_Click(object sender, EventArgs e)
{
string latitudeAsStr = string.Empty;
string longitudeAsStr = string.Empty;
List<MapDetails> lstMapDetails = new List<MapDetails>();
MapDetails md;
. . .
try
{
Form1.currentMap = mapName;
string qry = "SELECT LocationName, Address1, Address2, City, StateOrSo, PostalCode, " +
"MapDetailNotes, Latitude, Longitude " +
"FROM CartographerDetail " +
"WHERE FKMapName = @FKMapName";
var con = new SqliteConnection(connStr);
con.Open();
SqliteCommand cmd = new SqliteCommand(qry, con);
cmd.Parameters.AddWithValue("@FKMapName", mapName);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
md = new MapDetails();
md.LocationName = reader.GetValue(LOC_NAME).ToString().Trim();
. . .
latitudeAsStr = reader.GetValue(LATITUDE).ToString();
if (string.IsNullOrEmpty(latitudeAsStr))
{
md.Latitude = 0.0;
}
else
{
md.Latitude = Convert.ToDouble(reader.GetValue(LATITUDE).ToString());
}
longitudeAsStr = reader.GetValue(LONGITUDE).ToString();
if (string.IsNullOrEmpty(longitudeAsStr))
{
md.Longitude = 0.0;
}
else
{
md.Longitude = Convert.ToDouble(reader.GetValue(LONGITUDE).ToString());
}
lstMapDetails.Add(md);
}
AddPushpinsToListOfPushpins(lstMapDetails);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// All pushpins have been loaded
this.Close();
}
其Form_Closed和关闭按钮代码为:
private void mdlDlgFrm_LoadExistingMap_FormClosed(object sender, FormClosedEventArgs e)
{
this.DialogResult = DialogResult.OK; // Would this be a problem, assuming "OK" is always okay?
}
private void btnClose_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
作为奖励,AddPushpinsToListOfPushpins() 代码 - 在循环中调用 AddPushpinToListOfPushpins() - 是:
private void AddPushpinsToListOfPushpins(List<MapDetails> lstMapDetails)
{
string fullAddress;
foreach (MapDetails _md in lstMapDetails)
{
fullAddress = string.Format("{0} {1} {2} {3}", _md.Address, _md.City, _md.StateOrSo, _md.PostalCode).Trim();
AddPushpinToListOfPushpins(_md.LocationName, fullAddress, _md.MapDetailNotes, _md.Latitude, _md.Longitude, _md.PushpinColor);
}
}
【问题讨论】:
-
另外,您不应该公开您的 API 密钥。
-
LOL - Heavens2MurgatroidAndSufferinSuccotash :) 希望这是更新的假钥匙。如果没有,请编辑您的问题。
-
这就是 git-go 的内容;是的,它是假的。
-
您能否发布
UpdateLocationWithCoordinates方法并说明如何/何时关闭mdlDlgFrm_LoadExistingMap对话框? -
@Reza:完成,发布了包含该代码(以及更多)的更新。谢谢。
标签: c# wpf winforms async-await bing-maps