经纬度函数的参数

3
我的主要目标是生成一个谷歌地图,显示特定建筑物的位置。由于谷歌的限制原因,我已经在 MS SQL 数据库中生成并存储了所有建筑物的纬度和经度(它是一个房地产网站)。每次选择一个建筑物后,我会检索其对应的纬度和经度,并将其存储在两个 asp:Label 中。我使用 Javascript 脚本来处理通过两个 asp:Label 传递的纬度和经度。我的问题是 LatLng 函数似乎不能正常工作,我的地图没有显示应该显示的坐标。我认为我可能遇到了 LatLng 需要的变量类型问题。我已经尝试过默认字符串和将变量转换为实数类型。以下是脚本。感谢任何帮助或建议:
  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(maplatitude, maplongitude);

              //window.alert("Processed propertyaddress");

              //latlng = new google.maps.LatLng(25.76804, -80.132743);

              // Creating an object literal containing the properties
              // we want to pass to the map
               myOptions = {
                  zoom: 15,
                  center: new google.maps.LatLng(maplatitude, maplongitude),
                  //center: buildinglatlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  scaleControl: true,
                  streetViewControl: true,
                  disableDefaultUI: true,
                  mapTypeControl: true,
                  mapTypeControlOptions: {
                      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                      position: google.maps.ControlPosition.TOP_LEFT,
                      mapTypeIds: [
                google.maps.MapTypeId.ROADMAP,
                google.maps.MapTypeId.TERRAIN,
                google.maps.MapTypeId.SATELLITE
            ]
                  },
                  navigationControl: true,
                  navigationControlOptions: {
                      position: google.maps.ControlPosition.TOP_LEFT
                  }
              };
              // Creating the map
              map = new google.maps.Map(document.getElementById("map"), myOptions);
          }
          window.onload = InitializeMap;
      })();
</script>

已添加可用的代码,但使用了地理编码器

例如,以下代码完美运行,但使用了地理编码器。传递变量不是问题。LatLng存在一些奇怪的问题,会影响传递的变量。但它确实获取了所有重要位置的值。

  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(maplatitude, maplongitude);

              //window.alert("Processed propertyaddress");

              //latlng = new google.maps.LatLng(25.76804, -80.132743);

              if (!geocoder) {
                  geocoder = new google.maps.Geocoder();
              }

              // Creating a GeocoderRequest object
              var geocoderRequest = {
                  address: propertyaddress
              }


              geocoder.geocode(geocoderRequest, function (results, status) {
                  // Check if status is OK before proceeding
                  if (status == google.maps.GeocoderStatus.OK) {
                      // Center the map on the returned location
                      //map.setCenter(results[0].geometry.location);
                      // Creating an object literal containing the properties
                      // we want to pass to the map
                      myOptions = {
                          zoom: 15,
                          //center: new google.maps.LatLng(maplatitude, maplongitude),
                          center: results[0].geometry.location,
                          mapTypeId: google.maps.MapTypeId.ROADMAP,
                          scaleControl: true,
                          streetViewControl: true,
                          disableDefaultUI: true,
                          mapTypeControl: true,
                          mapTypeControlOptions: {
                              style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                              position: google.maps.ControlPosition.TOP_LEFT,
                              mapTypeIds: [
                                google.maps.MapTypeId.ROADMAP,
                                google.maps.MapTypeId.TERRAIN,
                                google.maps.MapTypeId.SATELLITE
                                ]
                          },
                          navigationControl: true,
                          navigationControlOptions: {
                              position: google.maps.ControlPosition.TOP_LEFT
                          }
                      };
                      // Creating the map
                      map = new google.maps.Map(document.getElementById("map"), myOptions); 

                      // Check to see if we've already got a Marker object
                      if (!marker) {
                          // Creating a new marker and adding it to the map
                          marker = new google.maps.Marker({
                              map: map,
                              animation: google.maps.Animation.DROP
                          });
                          google.maps.event.addListener(marker, 'click', toggleBounce);
                      }
                      // Setting the position of the marker to the returned location
                      marker.setPosition(results[0].geometry.location);
                      // Check to see if we've already got an InfoWindow object

                      google.maps.event.addListener(marker, 'click', function () {
                          if (!infowindow) {
                              // Creating a new InfoWindow
                              infowindow = new google.maps.InfoWindow();
                          }
                          // Creating the content of the InfoWindow to the address
                          // and the returned position
                          var content = '<h2>' + selectedbuilding + '</h2>';
                          //content += 'Lat: ' + results[0].geometry.location.lat() + '<br />';
                          //content += 'Lng: ' + results[0].geometry.location.lng();
                          // Adding the content to the InfoWindow
                          infowindow.setContent(content);
                          // Opening the InfoWindow
                          infowindow.open(map, marker);
                      });

                      // Triggering the click event
                      google.maps.event.trigger(marker, 'click');
                  };
              });



          }

          function toggleBounce() {
              if (marker.getAnimation() != null) {
                  marker.setAnimation(null);
              } else {
                  marker.setAnimation(google.maps.Animation.BOUNCE);
              }
          }

          window.onload = InitializeMap;
      })();
</script>

问候, 埃利亚斯


1
问题可能出在您对 innerText 的使用上(请参见 https://dev59.com/OnM_5IYBdhLWcg3wfTI0)。如果您 alert maplatitudemaplongitude,会发生什么? - Tim M.
不,它不是innerText,因为我正在传递地址,例如以下代码可以完美地工作,但是使用geocoder会被收取很多费用: - Elias Echeverri
2个回答

0
我刚在 Google 找到一篇文章,他们更改了 LatLng 函数,现在你必须使用 NUMBER() 将两个参数强制转换为数字。他们声称人们将字符串传递给 LatLng 函数,从而创建了不可预测的结果。所以我附加了我的新代码和更改...只用了三天就找到了这个解决方法!!! :-( 我想知道为什么没有更多的人遇到这个问题。这是代码:
  <script type="text/javascript">
      (function () {

          // Defining global variables
          var map, geocoder, marker, infowindow, propertyaddress, selectedbuilding, maplatitude, maplongitude, buildinglatlng, latlng, myOptions;

          function InitializeMap() {
              //propertyaddress = '400 Alton Road, Miami Beach, FL 33139';
              propertyaddress = document.getElementById('<%=lblselectedHiddenBuildingAddress.ClientID%>').innerText;
              selectedbuilding = document.getElementById('<%=lblMainBuilding.ClientID%>').innerText;

              //maplatitude = parseFloat(document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText);
              //maplongitude = parseFloat(document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText);
              maplatitude = document.getElementById('<%=lblCoordinateLatitud.ClientID%>').innerText;
              maplongitude = document.getElementById('<%=lblCoordinateLongitud.ClientID%>').innerText;
              buildinglatlng = new google.maps.LatLng(Number(maplatitude), Number(maplongitude));

              //window.alert("Processed propertyaddress");

                      myOptions = {
                          zoom: 15,
                          center: new google.maps.LatLng(Number(maplatitude), Number(maplongitude)),
                          mapTypeId: google.maps.MapTypeId.ROADMAP,
                          scaleControl: true,
                          streetViewControl: true,
                          disableDefaultUI: true,
                          mapTypeControl: true,
                          mapTypeControlOptions: {
                              style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                              position: google.maps.ControlPosition.TOP_LEFT,
                              mapTypeIds: [
                                google.maps.MapTypeId.ROADMAP,
                                google.maps.MapTypeId.TERRAIN,
                                google.maps.MapTypeId.SATELLITE
                                ]
                          },
                          navigationControl: true,
                          navigationControlOptions: {
                              position: google.maps.ControlPosition.TOP_LEFT
                          }
                      };
                      // Creating the map
                      map = new google.maps.Map(document.getElementById("map"), myOptions); 

                      // Check to see if we've already got a Marker object
                      if (!marker) {
                          // Creating a new marker and adding it to the map
                          marker = new google.maps.Marker({
                              map: map,
                              animation: google.maps.Animation.DROP
                          });
                          google.maps.event.addListener(marker, 'click', toggleBounce);
                      }
                      // Setting the position of the marker to the returned location

                      marker.setPosition(buildinglatlng);

                      // Check to see if we've already got an InfoWindow object

                      google.maps.event.addListener(marker, 'click', function () {
                          if (!infowindow) {
                              // Creating a new InfoWindow
                              infowindow = new google.maps.InfoWindow();
                          }
                          // Creating the content of the InfoWindow to the address
                          // and the returned position
                          var content = '<h2>' + selectedbuilding + '</h2>';

                          // Adding the content to the InfoWindow
                          infowindow.setContent(content);
                          // Opening the InfoWindow
                          infowindow.open(map, marker);
                      });

                      // Triggering the click event
                      google.maps.event.trigger(marker, 'click');

          }

          function toggleBounce() {
              if (marker.getAnimation() != null) {
                  marker.setAnimation(null);
              } else {
                  marker.setAnimation(google.maps.Animation.BOUNCE);
              }
          }

          window.onload = InitializeMap;
      })();
</script>

0
你验证过 maplatitudemaplongitude 是否包含你期望的值了吗(比如使用调试器)?
预期位置和地图上显示的实际位置之间是否存在关联(例如,如果建筑位于N21.7684,而标记放置在N21.0000,则可能已丢失小数位)。

是的,我已经彻底调试过了,并追踪了LatLng在setcenter或center赋值之前包含所需内容的事实:但它就是不起作用。当我使用geocoder时,做同样的事情它就可以工作。 - Elias Echeverri
请看一下我刚刚放上去的新代码,它使用了地理编码器并且完美运行。但是如果我使用这个实现方式,我将会被收取很高的费用 :-)。欢迎提出任何想法。 - Elias Echeverri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接