0

这个问题已经解决了,即使答案与标题无关。这是一个结构糟糕的问题,令我失望的是,我无法按原样删除它。感谢您的帮助并查看代码。

我使用谷歌地图为我的应用程序提供了 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(window, 'load', 初始化);

我这样做的原因是因为我似乎无法得到latlng. 我最初所做的是初始化地图window.onload = function(){ initialize() }并调用 .ajax 函数,$(window).load(function(){...});无论哪种方式我都无法正确处理。

抱歉,如果这听起来令人困惑。如果这还不够,我会添加更多细节。

编辑:

results[0].geometry.location是通过反向地理编码 6 位邮政编码产生的纬度经度。我想要完整的地址,但只有 6 位邮政编码。纬度和经度也用于在谷歌地图上放置标记。因此,为了获得完整的地址,我试图使用 google 提供的 url 获得 json 结果。

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

4

1 回答 1

0

假设 results[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";

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

于 2012-07-11T08:12:13.443 回答