简体   繁体   English

从 MySQL 数据库加载纬度/经度并使用 PHP、html 和 JavaScript 在谷歌地图上显示点

[英]Loading latitude/longitude from MySQL database and showing point on Google Maps using PHP,html and JavaScript

I googled a lot but all the solutions i found didn't work for me (even google maps tutorial).我用谷歌搜索了很多,但我发现的所有解决方案都不适合我(甚至谷歌地图教程)。 I have a MySQL database where I store latitude and longitude values in Float type columns.我有一个 MySQL 数据库,我将纬度和经度值存储在 Float 类型列中。 I wrote php script which connects to the database and selects those values.我编写了连接到数据库并选择这些值的 php 脚本。 The idea is that php script passes an array of longitude and latitude values to JavaScript and then Javascript iterate on that array making new markers on Google Maps.这个想法是 php 脚本将一组经度和纬度值传递给 JavaScript,然后 Javascript 迭代该数组,在谷歌地图上制作新标记。 Here is my actual code:这是我的实际代码:

 <html> <head> <style> #map { width: 100%; height: 700px; background-color: grey; } </style> </head> <body> <?php $query ="SELECT latitude,longitude FROM markers"; $result_array=Array(); $con = new mysqli($host, $user, $password, $dbname, $port) or die ('Could not connect to the database server' . mysqli_connect_error()); $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ $latitude=$row['latitude']; $longitude=$row['longitude']; $result_array[]=array('latitude'=>$latitude,'longitude'=>$longitude); } $json=json_encode($result_array); $con->close(); ?> <h3>MyMap</h3> <div id="map"></div> <script> function initMap() { var mapDiv = document.getElementById('map'); var map = new google.maps.Map(mapDiv, { center: {lat: 41.5400 , lng: 12.2500}, zoom: 6 }); var array="<?php echo $json_array;?>"; for(var i=0;i<array.length;i++){ var myLatLng=new google.maps.LatLng(lat,lon); var marker=new google.maps.Marker({ map:map, position: myLatLng, }); //} } </script> <script async defer src=MY GOOGLE API MAP> </script> </body> </html>

Can anyone help me or suggest other ways to do the same thing?任何人都可以帮助我或建议其他方法来做同样的事情吗?

Try putting newlines between <?php and the actual content, like so:尝试在<?php和实际内容之间放置换行符,如下所示:

var array=JSON.parse("<?php
          echo $json_array;
          ?>");

The PHP substitution should put the double quotes on the same line. PHP 替换应该将双引号放在同一行上。

 <html> <head> <style> #map { width: 100%; height: 700px; background-color: grey; } </style> </head> <body> <?php $query ="SELECT latitude,longitude FROM markers"; $result_array=Array(); $con = new mysqli($host, $user, $password, $dbname, $port) or die ('Could not connect to the database server' . mysqli_connect_error()); $result = mysqli_query($con,$query); while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ $latitude=$row['latitude']; $longitude=$row['longitude']; $result_array[]=array('latitude'=>$latitude,'longitude'=>$longitude); } $json=json_encode($result_array); $con->close(); ?> <h3>MyMap</h3> <div id="map"></div> <script> function initMap() { var mapDiv = document.getElementById('map'); var map = new google.maps.Map(mapDiv, { center: {lat: 41.5400 , lng: 12.2500}, zoom: 6 }); var array="<?php echo $json;?>";//here changed for(var i=0;i<array.length;i++){ var myLatLng=new google.maps.LatLng(lat,lon); var marker=new google.maps.Marker({ map:map, position: myLatLng, }); //} } </script> <script async defer src=MY GOOGLE API MAP> </script> </body> </html>

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

相关问题 使用MySQL数据库中的纬度和经度值在Google地图上添加标记 - Adding Markers on Google maps using Latitude and Longitude Values from MySQL Database 通过单击Google Maps HTML获取特定点的纬度和经度 - Get latitude and longitude of a certain point by a click in Google Maps HTML Google Maps使用PHP,MySQL显示错误的经度值 - Google Maps showing wrong Longitude value using PHP, MySQL 使用经度和纬度在Google地图中定位位置 - Using latitude and longitude to pin point a location in google maps 从纬度和经度值构建时,谷歌地图不显示路线 - Google Maps not showing route when building from latitude and longitude values 从mysql检索经度,并将其传递给Google Maps API - Retrieving latitude and longitude from mysql, passing it to google maps api 如何将经度从HTML表单传递到Google Maps - How to pass Latitude Longitude from HTML form to Google Maps Javascript 查找从谷歌地图获取纬度和经度 - Javascript to find get latitude and longitude from Google Maps 如何从函数获取纬度经度并将其发送到新的google.maps.LatLng(latitude,longitude)javascript - how to get latitude longitude from function and send it to new google.maps.LatLng( latitude ,longitude ) javascript 如何获取经度和纬度的谷歌在Javascript地图 - how to get longitude and latitude of google maps in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM