也许这听起来很愚蠢,但我真的无法弄清楚如何在我的.aspx页面中通过JavaScript设置<img>
代码。
到目前为止我的代码:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Teste.aspx.vb" Inherits="Teste" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Latitude: <asp:TextBox ID="nr_latitudeTextBox" runat="server" MaxLength="12" /> <br />
Longitude: <asp:TextBox ID="nr_longitudeTextBox" runat="server" MaxLength="12" /><br />
<asp:Button ID="cmb_mapa" runat="server" Text="Mapa" OnClick="cmb_mapa_Click"/><br />
<script language="javascript" type="text/javascript">
function fonte(lat, lon) {
var src = "http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false";
testando("http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false", 540, 280, "Mapa");
}
function testando(src, width, height, alt) {
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height;
img.alt = alt;
document.body.appendChild(img);
}
</script>
<div id="img">
</div>
</asp:Content>
部分课程:
Partial Class Teste
Inherits System.Web.UI.Page
Protected Sub cmb_mapa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_mapa.Click
Page.ClientScript.RegisterStartupScript(Me.GetType(), "testando", "testando(" & nr_latitudeTextBox.Text & "," & nr_longitudeTextBox.Text & ");", True)
End Sub
End Class
答案 0 :(得分:2)
testando
第一个参数是图片网址,但您没有提供。
您似乎想在fonte
上致电testando
而不是RegisterStartupScript
。这是将纬度和经度作为参数的方法。
修改强>
更改
var src = "http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false"
testando("http://maps.google.com/maps/api/staticmap?center=lat,lon&zoom=8&size=540x280&maptype=roadmap&sensor=false", 540, 280, "Mapa");
通过
var src = "http://maps.google.com/maps/api/staticmap?center=" + lat + "," + lon + "&zoom=8&size=540x280&maptype=roadmap&sensor=false";
testando(src, 540, 280, "Mapa");
修正了两件事:
1)您没有使用lat
和lon
作为地图网址的参数,因此已修复src
建筑物
2)添加src
作为testeando
的参数,因为您没有使用它