google.maps.setCenter() 纬度 &通过 jquery.ajax() 请求经度
我有一个 jquery 自动完成 ui 元素,显示火车站列表。当用户从自动完成列表中选择一个站点时,该函数应该从数据库返回一组纬度/坐标,并将地图重新放置在这些坐标上?
有人发现我这段代码哪里错了吗?
// make a json request to get the map data from the Map action
$(function() {
$.getJSON("/Home/Map", initialise);
});
var infowindow = new google.maps.InfoWindow({content: "EMPTY"});
function initialise(mapData) {
var latlng = new google.maps.LatLng(54.466667, -3.233333);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.TERRAIN,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
scaleControl: true,
streetViewControl: false
};
var map = new google.maps.Map($("#map")[0],
myOptions);
$.each(mapData, function (i, location) {
setupLocationMarker(map, location);
});
//Autocomplete
$(function () {
$("#tags").autocomplete({
source: "/Home/StationsList",
minLength: 2,
select: function (event, ui) {
jQuery.ajax({
url: "Home/GetStationLatLong/" + ui.item.value,
dataType: "json",
success: function (data) {
map.setCenter(new google.maps.latLng(data.latitude, data.longitude));
}
});
}
});
});
}
function setupLocationMarker(map, location) {
var latlng = new google.maps.LatLng(location.Latitude, location.Longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: location.Name
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent('<h2>' + location.Name + '</h2>');
infowindow.open(map, marker);
$("#info").text(location.Name);
});
}
“Home/GetStationLatLong”请求从服务器返回的 JSON 如下所示
[{"latitude":53.66314,"longitude":-1.48149}]
I've got an jquery autocomplete ui element that shows a list of rail stations. When a user selects a Station from the autocomplete list, the function should return a set of latitude/Coordinates from the db and recenter the map over those cordinates?
Anyone spot where i'm code wrong in this code?
// make a json request to get the map data from the Map action
$(function() {
$.getJSON("/Home/Map", initialise);
});
var infowindow = new google.maps.InfoWindow({content: "EMPTY"});
function initialise(mapData) {
var latlng = new google.maps.LatLng(54.466667, -3.233333);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.TERRAIN,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
scaleControl: true,
streetViewControl: false
};
var map = new google.maps.Map($("#map")[0],
myOptions);
$.each(mapData, function (i, location) {
setupLocationMarker(map, location);
});
//Autocomplete
$(function () {
$("#tags").autocomplete({
source: "/Home/StationsList",
minLength: 2,
select: function (event, ui) {
jQuery.ajax({
url: "Home/GetStationLatLong/" + ui.item.value,
dataType: "json",
success: function (data) {
map.setCenter(new google.maps.latLng(data.latitude, data.longitude));
}
});
}
});
});
}
function setupLocationMarker(map, location) {
var latlng = new google.maps.LatLng(location.Latitude, location.Longitude);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: location.Name
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent('<h2>' + location.Name + '</h2>');
infowindow.open(map, marker);
$("#info").text(location.Name);
});
}
The JSON Returned from the server by the "Home/GetStationLatLong" request looks like this
[{"latitude":53.66314,"longitude":-1.48149}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
try
也许您正在寻找这个
Maybe your looking for this
成功函数应该是这样的:
The success function should be like: