即使答案與標題無關,也可以解決此問題。這是一個結構嚴密的問題,對於我的失望,我無法將其刪除。感謝您的幫助和查看代碼。
我有我的應用程序使用谷歌地圖2正常的JavaScript函數和一個jQuery的功能。簡言之,它看起來像這樣
<script type="text/javascript">
window.onload = function()
{
initialize();
}
</script>
<script type="text/javascript">
var geocoder;
var markerE;
var theLocation;
var myLatlng;
var infowindow = new google.maps.InfoWindow();
var markersArray = [];
var eventMarkerArr = [];
var retrievedLatLng;
var retrievedLatLngArr = [];
var map;
$(document).ready(function(){
$('#tabs a').click(function(e){
e.preventDefault();
$(this).tab('show');
if($(this).attr("href").substring(1)=='event')
{
show('event');
hide('location');
}
else if($(this).attr("href").substring(1)=='location')
{
show('location');
hide('event');
}
});
});
function initialize() {
geocoder = new google.maps.Geocoder();
var myLatlng = new google.maps.LatLng(1.3667, 103.7500);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles:[{
featureType: "poi.attraction",
stylers: [
{ visibility: "off" }
]},{
featureType: "poi.business",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.government",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.medical",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.park",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.place_of_worship",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.school",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi.sports_complex",
stylers: [
{ visibility: "off" }
]
},{
}
]
};
map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
if (postalArr) {
for (var i = 0; i < postalArr.length; i++) {
codeAddress(postalArr[i]);
}
}
else
{
document.getElementById("event_list").value = "There are no events!";
}
var locationPin = 'images/green-pin.png';
}
var address;
var eventPin = "images/blue-pin.png";
function codeAddress(postal) {
geocoder.geocode({'address': postal + ", Singapore"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
retrievedLatLng = results[0].geometry.location;
var eventwindow = new google.maps.InfoWindow();
var markerE = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: eventPin,
});
markerE.mycategory = "event";
markerE.setVisible(false);
google.maps.event.addListener(markerE, 'click', function(){
eventwindow = new google.maps.InfoWindow({
content: "hello",
});
eventwindow.open(map,marker);
});
$.ajax({
type:"GET",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+retrievedLatLng+"&sensor=false",
dataType: "jsonp",
success: function(json){
if(json.status == 'OK')
{
alert("success");
}
}
});
eventMarkerArr.push(markerE);
retrievedLatLngArr.push("codeAddress:"+retrievedLatLng);
alert(retrievedLatLngArr[0]);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(窗口, '負載',初始化);
爲什麼我這樣做的原因是因爲我似乎無法得到latlng
。我最初做的是用window.onload = function(){ initialize() }
初始化地圖,然後在$(window).load(function(){...});
中調用.ajax函數,但我無法通過任何方式使它正確。
對不起,如果這聽起來很混亂。如果這還不夠,我會添加更多細節。
編輯:
results[0].geometry.location
是從反向地理編碼6位郵政編碼產生的緯度經度。我想要完整的地址,但只有6位數的郵政編碼。緯度和經度也用於在谷歌地圖上放置一個標記。因此,要獲得完整的地址,我試圖使用谷歌提供的網址獲得json結果。
更多編輯: 增補的碼全長
什麼是'results'?你的'.ajax()'調用沒有成功回調嗎?在發佈代碼時刪除不必要的細節是很好的,但在這種情況下,您已經刪除了太多的代碼。 (並注意「jQuery函數」_is_是一個正常函數。) – nnnnnn 2012-07-11 08:00:46
您需要將latlng分成lat +「,」+ lng – Steen 2012-07-11 08:01:13
您的示例非常不完整。什麼是'someAddress'?什麼是「結果」?你真正的問題是什麼? – 2012-07-11 08:02:19