在javascript函数中调用$ .ajax

时间:2012-07-11 07:57:35

标签: javascript jquery google-maps-api-3

这个问题已经解决,即使答案与标题无关。这是一个结构严重的问题,令我失望的是我不能删除它。感谢您的帮助并查看代码。

我使用谷歌地图为我的应用程序提供了2个普通的javascripts函数和一个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位数的邮政编码。纬度和经度用于在谷歌地图上放置标记。因此,要获取完整地址,我正在尝试使用google提供的网址获取json结果。

更多编辑: 添加了完整的代码

1 个答案:

答案 0 :(得分:0)

确定结果[0] .geometry.location是一个有效的位置。我不认为这是事实,但你可能已经有了这个

  url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+results[0].geometry.location.lat() +","+results[0].geometry.location.leg()+"&sensor=false";

您可能想要检查逗号分隔是否正确