javascript 使用 PHP 创建 Google 地图标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8314447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:01:18  来源:igfitidea点击:

Create Google Map Markers with PHP

phpjavascriptmysqlgoogle-mapsgoogle-maps-markers

提问by user1048676

All, I've got some locations stored in my database with long and lat coordinates. I'm debating between creating a map for each one of the locations or adding markers for all of them. Any advice would be appreciated on that idea but my issue is that I'm getting these values from my mySQL database. It looks like the only way you can actually add these is through javascript. Do I need to pass in the coordinates to a javascript function to create the markers? How would that look? Any suggestions are appreciated!

所有,我已经在我的数据库中存储了一些带有长坐标和纬度坐标的位置。我正在为每个位置创建地图或为所有位置添加标记之间进行辩论。关于这个想法的任何建议将不胜感激,但我的问题是我从 mySQL 数据库中获取这些值。看起来您实际上可以添加这些的唯一方法是通过 javascript。我是否需要将坐标传递给 javascript 函数来创建标记?那会怎样?任何建议表示赞赏!

回答by zrvan

You're correct. You have to fetch the data from the database and use something along the line of:

你是对的。您必须从数据库中获取数据并使用以下内容:

  function initialize() {
    var myLatlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>);
    var myOptions = {
      zoom: 4,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hello World!"
    });   
  }

In a < script type="text/javascript">-tag on the page. The above is taken from http://code.google.com/apis/maps/documentation/javascript/examples/marker-simple.html, so you'll have to adapt it to your needs.

在页面上的 <script type="text/javascript">-tag 中。以上摘自http://code.google.com/apis/maps/documentation/javascript/examples/marker-simple.html,因此您必须根据自己的需要进行调整。

回答by Jim DeLaHunt

Yes, you need to pass the long and lat coordinates from your database into Javascript code on the web page in order to create markers for them on the map. If you are using PHP code to generate the page, that means writing PHP code to query the database and then echo() the HTML page with included Javascript code.

是的,您需要将数据库中的经纬度坐标传递到网页上的 Javascript 代码中,以便在地图上为它们创建标记。如果您使用 PHP 代码生成页面,这意味着编写 PHP 代码来查询数据库,然后使用包含的 Javascript 代码 echo() HTML 页面。

zrvan's reply above uses new google.maps.LatLng()to create a single coordinates object and draw a single marker. If you decide to place multiple markers on a single map, I recommend putting the data for the markers into a Javascript array data structure. Then write a Javascript loop to call new google.maps.Marker()for each entry in the array.

zrvan 上面的回复new google.maps.LatLng()用于创建单个坐标对象并绘制单个标记。如果您决定在一张地图上放置多个标记,我建议将标记的数据放入 Javascript 数组数据结构中。然后编写一个 Javascript 循环来调用new google.maps.Marker()数组中的每个条目。

Google Maps documentation recommends using an XML document and AJAX calls to deliver the marker data from the database to the web page. See http://code.google.com/apis/maps/articles/phpsqlajax.html.

Google Maps 文档建议使用 XML 文档和 AJAX 调用将标记数据从数据库传送到网页。请参阅http://code.google.com/apis/maps/articles/phpsqlajax.html

If you get more than a few hundred markers on a single map, you might want to read the paper, "Too Many Markers!", in the Google Maps API documentation: http://code.google.com/apis/maps/articles/toomanymarkers.html.

如果在一张地图上有几百个标记,您可能需要阅读 Google Maps API 文档中的论文“标记太多!”:http: //code.google.com/apis/maps/文章/toomanymarkers.html