J'ai un élément jquery autocomplete ui qui montre une liste de stations de chemin de fer. Lorsqu'un utilisateur sélectionne une station dans la liste de saisie semi-automatique, la fonction doit renvoyer un ensemble de latitude/coordonnées à partir de la base de données et recentrer la carte sur ces coordonnées.google.maps.setCenter() latitude et longitude demandées via jquery.ajax()
Quelqu'un d'endroit où je suis mauvais code dans ce 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);
});
}
Le JSON renvoyé du serveur par le "Home/GetStationLatLong" demande ressemble à ce
[{"latitude":53.66314,"longitude":-1.48149}]
A bientôt, que [0] a fait marcher la réponse de jorge. Upvoted – MrBliz
Maintenant vous a marqué comme la bonne réponse car je demandais setCenter en premier lieu – MrBliz