此代码显示自动完成地址,但我想在地图上显示它们 我想通过从自动完成文本框中获取From和To地址在我的asp.net页面上添加Google地图。
请帮助我。
<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function () {
var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
var places2 = new google.maps.places.Autocomplete(document.getElementById('txtPlaces2'));
google.maps.event.addListener(places,places2, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.k;
var longitude = place.geometry.location.D;
var mesg = "Address: " + address;
mesg += "\nLatitude: " + latitude;
mesg += "\nLongitude: " + longitude;
// alert(mesg);
});
});
</script>
<span>Location 1:</span>
<input type="text" id="txtPlaces" style="width: 250px" /><br /><br />
<span>Location 2:</span>
<input type="text" id="txtPlaces2" style="width: 250px" />
&#13;
答案 0 :(得分:0)
您的addListener
电话似乎无效。第二个参数必须是事件,在这里你传递一个dom元素。所以我建议进行以下更改(未经我测试):
var from = null;
var to = null;
google.maps.event.addListener(places, 'place_changed', function() {
from = places.getPlace();
});
google.maps.event.addListener(places2, 'place_changed', function() {
to = places2.getPlace();
});
document.getElementById("submit").addEventListener("click", function() {
if(from == null) {
alert("You have to select an origin");
} else {
if(to == null) {
alert("You have to select a destination");
} else {
var start = from.geometry.location;
var end = to.geometry.location;
//Render the direction
}
}
});
答案 1 :(得分:0)
使用javascript和html代码检查this autocomplete gmap example。 如果您愿意,可以使用asp网络工具轻松更改它们。 此外this autocomplete example填写了地址表单。 对于Google地图,这是the documentation for auto compete。 您可以了解属性的工作原理。我建议您使用google的示例。
答案 2 :(得分:0)
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function () {
var places = new google.maps.places.Autocomplete(document.getElementById('txtfromaddress'));
google.maps.event.addListener(places, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.k;
var longitude = place.geometry.location.D;
var mesg = "Address: " + address;
mesg += "\nLatitude: " + latitude;
mesg += "\nLongitude: " + longitude;
alert(mesg);
});
});
</script>
<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function () {
var places = new google.maps.places.Autocomplete(document.getElementById('txttoaddress'));
google.maps.event.addListener(places, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.k;
var longitude = place.geometry.location.D;
var mesg = "Address: " + address;
mesg += "\nLatitude: " + latitude;
mesg += "\nLongitude: " + longitude;
alert(mesg);
});
});
</script>
<asp:Label id="lblfromaddress " runat="server"></asp:Label>
<asp:TextBox id="txtfromaddress" Runat="server" ></asp:TextBox>
<asp:Label id="lbltoaddress " runat="server"></asp:Label>
<asp:TextBox id="txttoaddress" Runat="server" ></asp:TextBox>
在使用后的代码中(C#):
lblfromaddress .Text =&#34;来自adddress&#34 ;;
lbltoaddress .Text =&#34;要添加地址&#34 ;;