我正在尝试使用Google maps API创建一个简单的代码(我是新手),其目的是从JSON变量direccion获取经度和经度到var uluru。 我有以下代码: 我哪里错了?
提前致谢。
//Repeater ItemTemplate RadioButton
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true"
OnCheckedChanged="RadioButton1_CheckedChanged"/>
//Code Behind
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
//Uncheck all RadioButtons of the rptCustomer
foreach (RepeaterItem item in rptCustomer.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
((RadioButton)item.FindControl("RadioButton1")).Checked = false;
}
}
//After foreach loop, set current selected checkbox checked
(sender as RadioButton).Checked = true;
}
答案 0 :(得分:2)
你的方向是json数组,你需要通过索引访问如下:
var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};
请参阅下面的示例:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var direccion=[
{
lugar:'CICESE',
lat:31.8675375,
lng: -116.6686867
},
{
lugar: 'Casa',
lat:31.8915163,
lng:-116.6879557
}
];
var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};//{lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDgMyVeh8DwebGfZHjiNjyle4xFe9pKSdc&callback=initMap">
</script>
</body>
</html>