2

Visual Studio 2010 を使用して asp.net アプリを作成しています。テキスト フィールドの文字列のメソッドを呼び出すテキスト フィールドとボタンがあります。

Enter your address here: <asp:TextBox ID="tb_address" runat="server" ></asp:TextBox>
<asp:Button Text="Submit" runat="server" onclick="GetLatLong" ></asp:Button>

ボタンの C# ファイルには、GetLatLong メソッドがあります。

protected void GetLatLong(object sender, EventArgs e)
{
    String address = tb_address.Text;
    String query = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
    address = address.Replace(" ", "+");
    query += address + "&sensor=false";
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(query);
    String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
    String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
}

緯度と経度の文字列を HTML ページに表示するにはどうすればよいですか?

4

4 に答える 4

3

sを使用<asp:Label />します。

<asp:Label ID="lblLat" runat="server" />
<asp:Label ID="lblLong" runat="server" />

String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
lblLat.Text = lat;
lblLong.Text = lon;
于 2012-08-29T17:44:11.387 に答える
3

結果を表示するためのコントロールを作成する必要があります (デザイン モードでフォームに追加するか、クリック イベント ハンドラーで動的に追加できます)。asp:Labels と言って、結果の値をそれらのラベルに割り当てます。

Label result1 = new Label();
result1.Text = lat;
this.Controls.Add(result1);

また

これをコードに入れます

<asp:Label ID='result1' runat='server' />

次に、コードビハインドから値を直接割り当てます。

result1.Text = lat;
于 2012-08-29T17:45:19.623 に答える
2

Literal値を保持するために、いくつかのコントロール (またはLabelコントロール、または任意の数の他のページ要素) を含めることができます。コントロールは次のようになります。

<asp:Literal runat="server" ID="LatitudeOutput" />
<asp:Literal runat="server" ID="LongitudeOutput" />

そして、コード ビハインドで値を設定します。

String lat = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
String lon = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
LatitudeOutput.Text = lat;
LatitudeOutput.Text = lon;

個人的Literalには、追加のマークアップを持ち込まないため、多くの場合、コントロールを好みます。 たとえば、Labelコントロールはタグでラップされます。spanしかし、多くのことと同様に、それを行う方法はたくさんあります。

于 2012-08-29T17:46:48.023 に答える
1

aspx ページにラベル コントロールを作成する必要があります。これを、lng と lat を表示する aspx ページに追加します。

<asp:Label ID="lblLat" runat="server" />
<asp:Label ID="lblLng" runat="server" />

次に、コードビハインドで

lblLat.Text = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
lblLng.Text = xDoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;

ラベルのテキストを SelectSingleNode 呼び出しで取得した値に設定しています。

于 2012-08-29T17:51:23.397 に答える