简体   繁体   English

热门从经纬度获取Google地图吗?

[英]Hot to get google map from lat and lng?

I know with the use of gmap v3 i can give the center, and add pointer and specify contents of info box and in that manner I can get my map to be generated. 我知道使用gmap v3可以指定中心,并添加指针并指定信息框的内容,这样我就可以生成地图。

but I want to know that is it possible to directly just give lat and lng and directly get my map generated and what ever is address at that place it will directly come in info box. 但我想知道是否可以直接提供lat和lng并直接生成我的地图,而该地点的地址将直接出现在信息框中。 Is there any way to do it directly? 有什么办法可以直接做到吗?

If you are generating coordinates using a file or database then use a php script to send an Ajax request to the following javascript file mapData.js . 如果要使用文件或数据库生成坐标,请使用php脚本将Ajax请求发送到以下javascript文件mapData.js I recommend sending jSON object named mapData that contains coordinates [lat, long] as follows. 我建议如下发送名为mapData jSON对象,该对象包含坐标[lat, long]

    google.load('visualization', '1', {'packages': ['map']});

  jQuery(document).ready(function($){
     $.getJSON("mapData.php", function(result)
   {
           var mapData=result;

     var dataTable1=new google.visualization.DataTable();
     dataTable1.addRows(mapData.length);

     dataTable1.addColumn('number', 'LATITUDE', 'Latitude');
     dataTable1.addColumn('number', 'LONGITUDE', 'Longitude');

      for(var i=0;i<mapData.length;i++) 
      {
        var coord=mapData[i]

        dataTable1.setValue(i, 0, parseFloat(coord[0]));
        dataTable1.setValue(i, 1, parseFloat(coord[1]));
     }

     var map=new google.visualization.Map(document.getElementById("Map1"));
         map.draw(dataTable1, {showTip: true, useMapTypeControl:true, enableScrollWheel:true, zoomLevel:19});   

   });   
 });

In your html do the following 在您的html中执行以下操作

    <head>
    <script src="https://apis.google.com/js/client.js"></script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script src="jQuery.js" type="text/javascript" ></script>
    <script src="mapData.js" type="text/javascript"></script>
 </head>

<body>

    <div  id="Map1">


    </div>  
</body>

More info here 更多信息在这里

I think you are still going to have to instantiate the new map before you can define the center, info boxes and pushpins etc. However, the javascript to do this is around 10 lines, so it is not too much and you could certainly put it into a method if you were to be calling it many times. 我认为您仍然必须实例化新地图,然后才能定义中心,信息框和图钉等。但是,执行此操作的javascript大约有10行,因此它并不是太多,您当然可以放进去如果您要多次调用它,可以将其转换为方法。 So effectively you would have a method which you provide a lon, lat to (perhaps a on the page also to render the map under) and then you would set the map. 如此有效地,您将拥有一种方法,可以提供一个纬度(可能在页面上还提供一个底下的地图),然后设置地图。

To center the map on a given lon, lat you can provide the map with the following option:- 要将地图放在给定的位置上,您可以为地图提供以下选项:-

center = new google.maps.LatLng(myLat, myLon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM