для начала Я только начал играть с API Карт Google (и использовал AJAX вместе с ним) ... Создал много проблем ... решил их. Но теперь я, кажется, застрял.показ сохраненных маркеров в API Карт Google
Я учился в Google Developers Tutorial о том, как создать Store-Locator (https://developers.google.com/maps/articles/phpsqlsearch_v3) - действительно хорошо написан и объяснен, но я немного менял его для своих нужд.
Теперь я получаю ошибку
InvalidValueError: setMap: not an instance of Map, and not an instance of StreetViewPanorama
Функция создания ошибки:
function downloadUrl(url,callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null); /* Console Points the Error to this line */
}
И это функция работает функция выше:
function showAll() {
var searchUrl = 'genxml.php';
downloadUrl(searchUrl, function(data) {
var xml = parseXml(data);
var markerNodes = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markerNodes.length; i++) {
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng"))
);
showMarker(latlng);
}
});
}
Мой genxml. php отлично работает, он генерирует XML-документ, который я вижу через FireBug.
Я дважды проверял каждую строку. Надеюсь, что кто-то найдет мою ошибку :) Если вам нужен дальнейший код, дайте мне знать!
Edit: - Добавлен код инициализации
function initialize() {
var hideInfos = [
{
featureType: "poi",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
// Map configurations
var mapOptions = {
zoom:12,
minZoom:12,
maxZoom:19,
disableDefaultUI: true,
styles: hideInfos,
center: new google.maps.LatLng(48.149137,11.5637916)
};
// Create a new Map with the config above; save object into "map" variable
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
// Borderpath configuration
var borderCoordinates = [
new google.maps.LatLng(48.096655, 11.474876),
new google.maps.LatLng(48.191954, 11.474876),
new google.maps.LatLng(48.191954, 11.655120),
new google.maps.LatLng(48.096655, 11.655120),
new google.maps.LatLng(48.096655, 11.474876)
];
// Create a new Polyline with config above; save object into "border"
var border = new google.maps.Polyline({
path: borderCoordinates,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
// Draw border on map
border.setMap(map);
// Create new LatLngBounds Object and save it into strictBounds
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(48.096655, 11.474876),
new google.maps.LatLng(48.191954, 11.655120)
);
// Listen for the dragend event and check if dragged out of strictBounds
google.maps.event.addListener(map, 'dragend', function() {
if (strictBounds.contains(map.getCenter())) return;
// We're out of bounds - Move the map back within the bounds
var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast().lng(),
maxY = strictBounds.getNorthEast().lat(),
minX = strictBounds.getSouthWest().lng(),
minY = strictBounds.getSouthWest().lat();
if (x < minX) x = minX;
if (x > maxX) x = maxX;
if (y < minY) y = minY;
if (y > maxY) y = maxY;
map.setCenter(new google.maps.LatLng(y, x));
});
// Place Marker where clicked
google.maps.event.addListener(map, 'click', function(e) {
placeMarker(e.latLng, map);
});
}
Обязательно! Отредактировано главное сообщение! Надеюсь, это поможет! : D – iR0Nic
Если я понял, вы пытаетесь вызвать переменную «map» вне функции «initialize». В том, что? Вам нужно создать экземпляр вне функции, сделав переменную глобальной. – ital0
@ ital0 - вы должны обновить свой ответ, который прямо сейчас просто читается как комментарий, чтобы включить то, что вы положили в свой следующий комментарий :-) – duncan