【问题标题】:Redrawing map based on lat and long google map基于经纬度谷歌地图重绘地图
【发布时间】:2012-10-19 09:16:09
【问题描述】:

我在下面的 URL 中有一个由以下脚本生成的谷歌地图

http://apryll.co.in/maps/index.php

变量位置 = [ ['', -33.890542, 151.274856, 4], ['', -33.923036, 151.259052, 5], ['', -34.028249, 151.157507, 3], ['', -33.80010128657071, 151.28747820854187, 2], ['', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { 缩放:10, 中心:新的 google.maps.LatLng(-33.92, 151.25), mapTypeId:google.maps.MapTypeId.ROADMAP }); var flagIcon_front = new google.maps.MarkerImage("images/marker.png"); var flagIcon_shadow = new google.maps.MarkerImage("images/marker_shadow.png") flagIcon_shadow.size = 新的 google.maps.Size(105, 53); flagIcon_shadow.anchor = 新的 google.maps.Point(20, 52); var boxText = document.createElement("div"); boxText.style.cssText = "边框:1px 纯黑色;上边距:8px;内边距:5px;显示:块;"; var myOptions = { 内容:boxText ,disableAutoPan: 假 ,最大宽度:0 ,pixelOffset: 新的 google.maps.Size(-261, -268) ,zIndex: 空 ,boxStyle: { 背景:“url('images/metro-plot-bg-1.png')无重复-286px -1361px” ,不透明度:1 ,宽度:“393px” ,高度:“233px” } ,closeBoxMargin: "11px 32px 2px 2px" ,closeBoxURL: "图片/close.png" ,infoBoxClearance: 新的 google.maps.Size(1, 1) ,isHidden: 假 ,窗格:“浮动窗格” ,启用事件传播:假 }; var infowindow = new google.maps.InfoWindow(); 变量标记,我; 对于 (i = 0; i

我在页面上有一个下拉菜单可以选择不同的位置。

http://apryll.co.in/maps/index.php

当我选择下拉菜单时,它会显示不同位置的纬度和经度 作为 ajax 调用的响应。

现在我想用响应文本中新收到的经纬度重新绘制谷歌地图

提前感谢

【问题讨论】:

  • 将新接收到的值传递到位置数组中。你试过了吗?
  • 这就是我被抓住的地方,如何将其传递给数组。页面没有刷新,我该怎么做
  • 您从哪里获得选择后警报窗口中的这些 latlng 值?
  • 我通过向同一页面发送 ajax 请求从数据库中获取这些

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


【解决方案1】:

不了解php,所以无法为您提供示例代码,但这是您可以做的-

当您在从组合框中进行选择时从数据库中获取纬度时,您还可以向其中添加此功能。

首先,您应该将标记放置代码移动到单独的函数中。从数据库中获取的值应该传递给一个数组。让这个数组只在这个函数中声明。然后该数组可用于放置标记,因为每次更改组合框中的选择时都会执行此函数。

您可以在我的Question 中看到一些 Asp.net 代码。希望对你有帮助。

【讨论】:

    【解决方案2】:
    Latlng = new google.maps.LatLng(some_lat,some_lon);
    map.panTo(Latlng);
    

    你也可以使用

    map.setCenter(Latlng);
    

    不同之处在于,如果原始纬度和经度与新经度足够接近,panTo 将使用漂亮的动画来执行此操作。

    在此处查看文档: https://developers.google.com/maps/documentation/javascript/reference

    【讨论】:

      【解决方案3】:

      为了解决上述问题,我做了以下事情。希望对你有帮助。

      1 我已经创建了两个独立的

      div with id="map" and id="map1".
      
      <div id="map" style="width: 700px; height: 400px;"></div>
      <div id="map1" style="width: 700px; height: 400px;"></div>
      
      1. 以下是我的地图脚本。

        <script type="text/javascript">
        
        window.onload = mapdata(38.8833,77.0167,<?php echo $jsonarray; ?>);
        function mapdata(lat,lng,data){
            var locations = data;
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 3,
              center: new google.maps.LatLng(lat,lng),
              mapTypeId: google.maps.MapTypeId.ROADMAP
            });
        
            var infowindow = new google.maps.InfoWindow();
        
            var marker, i;
            for (i = 0; i < locations.length; i++) {  
              marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map
              });
        
              google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                  infowindow.setContent(locations[i][0]);
                  infowindow.open(map, marker);
                }
              })(marker, i)); 
            }
        }
        </script>
        

      是我在地图中显示的数据数组。

      3.

      <script> 
      $(document).on("click",'#search',function(){
          var zipcode=$("#zipcode").val();
          if (zipcode!='') {
              var market=$("#market").val();
              var country=$("#country").val();
              jQuery.ajax({ //passing value using the jquery
                 type:"POST",
                 data:"zipcode="+zipcode+"&market="+market+"&country="+country,           
                 dataType:'json', 
                 url: "<?php echo Yii::app()>createUrl('/front/front/mapsearch/')?>",
                 success: function(data){
                      if(data.detail != ''){
                          mapdata(data.lat,data.lng,data.detail);
                      }else{
                          $("#map").hide();
                          $("#map1").show();
                          $("#map1").html("Data Not Found...!!");
                      }
                 }
             });
          }       
      });
      </script>
      

      现在当用户更改下拉值时,我将我的变量传递到控制器中,而不是将响应从控制器传递到查看文件。并调用mapdata函数重新生成地图。

      下面是我返回的数组。

      $result = array('lat'=>$lat,'lng'=>$lng,'detail'=>$marker);
      $jsonarray = json_encode($result);
      echo $jsonarray; 
      

      【讨论】:

        【解决方案4】:

        您必须使用 google.maps.LatLngBounds() 创建一个边界,然后使用您的位置数组对其进行扩展。

        bounds = new google.maps.LatLngBounds();
        for(var count=0;count<locations.length;count++){
            bounds.extend(new google.maps.LatLng(locations[count][1],locations[count[2]);
        }
        map.fitBounds(bounds);
        

        如果你想变得花哨,你也可以使用 map.panToBounds(bounds)

        然后在某个地方

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-08
          相关资源
          最近更新 更多