【问题标题】:Address into database on google map地址到谷歌地图上的数据库
【发布时间】:2015-01-13 12:52:57
【问题描述】:

我的数据库中有一些人的地址(在名为任务的表中,字段是地址)

我还有一个地理编码器功能,可以将此地址更改为 long 和 lat 以在我的地图上绘制标记。

我唯一不明白的就是从我的数据库中获取我的地址并将其用于我的地图

这是我的代码:(编辑)

我想从我的数据库中获取地址并将其放入我的地理编码器函数的“地址”字段中。但这有可能做到吗?如果是的话怎么办?

感谢您的帮助。 (希望我清楚^^')

编辑:

我的 .js

<script type="text/javascript">
    // When the window has finished loading create our google map below

    var map;
    var geocoder;

    function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapOptions = {
            zoom: 14
        };
        map = new google.maps.Map(document.getElementById('map-geo'), mapOptions);

        if(navigator.geolocation) {
            var circle = new google.maps.Circle({
                    center: new google.maps.LatLng(52.376018, 4.901962),
                    radius: 500,
                    map: map
                });

            navigator.geolocation.getCurrentPosition(function(position) {
                var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                accuracy = parseFloat(position.coords.accuracy);
                map.setZoom(14);
                circle.setCenter(pos);
                circle.setRadius(accuracy);
                map.fitBounds(circle.getBounds());
                var infowindow = new google.maps.InfoWindow({
                    map: map,
                    position: pos,
                    content: 'Voici votre géolocalisation'
                });

            //map.setCenter(pos);
            }, function() {
            circle.setMap(null);
            handleNoGeolocation(true);
        });
        } else {
            // Browser doesn't support Geolocation
            handleNoGeolocation(false);
        }
        $.ajax({    //create an ajax request to a page that prints the address.
            url: "addMarkers.php",  //url for the address page                    
            success: function(result){
                var adresse = result; //create a variable and give it the value of the response data
                codeAddress(adresse); //decode the address using codeAddress into Lat and Lon
            }
        });
        codeAddress(<?php echo $adresse; ?>);
        //codeAddress();
    }

    function codeAddress(adresse) {
      geocoder.geocode( { 'address': "61 rue de la libération, 44230, saint sebastien sur loire"}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var marker = new google.maps.Marker({
              map: map,
              position: results[0].geometry.location
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }

    function handleNoGeolocation(errorFlag) {
      if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
      } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
      }
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

还有我的 .php

<?php
require_once("../admin/php/checklogin.php");
$host_name  = "my_host";
$database   = "my_db";
$bdduser_name  = "my_name";
$bddpassword   = "my_bddpass";

mysql_connect($host_name, $bdduser_name, $bddpassword) or die ("connection impossible.");
mysql_select_db($database) or die ("connection bdd impossible.");
//tu fais la connexion à la base puis:

$sql="SELECT `id`, `nom`, `adresse`, `esc`, `cp`, `ville`
FROM `missions` WHERE 1"; //remplaces par tes noms

ini_set('mysql.trace_mode', true); 
mysql_set_charset('utf8'); 

$result = mysql_query($sql); 
 ?>

【问题讨论】:

  • 仅供参考,adress 应为 address(英文)。
  • 是的,我在所有的解释中只放了一个 d,但我在代码中有 2 个 d :)

标签: javascript php mysql google-maps google-maps-api-3


【解决方案1】:

首先您可以使用AJAX 请求(建议使用jQuery 代表this)到服务器上的php 文件来检索地址信息。

$.ajax({    //create an ajax request to a page that prints the address.
    url: "getAddress.php",  //url for the address page                    
    success: function(response){                    
        var address = response; //create a variable and give it the value of the response data
        codeAddress(address); //decode the address using codeAddress into Lat and Lon
    }
});

在我看来,第二种但不太干净的方法是太直接地将地址变量从 php 回显到 javascript。

   codeAddress(<?php echo $address; ?>);

我希望我为您指明了正确的方向。如果您对 PHP 和 Javascript 有一定的了解,您应该能够实现您的目标。

【讨论】:

  • 谢谢兄弟提供的例子,我会在你发布它时尝试,如果它工作后回复;-)
  • 我编辑了我的答案。祝你的项目好运,记得享受有趣的编程:)
  • 绝对选择选项 1!
  • @koenvdheuvel 我编辑了我的第一篇文章,这次使用的是 .php ^^。我忘记了要放入我的 js 文件的邮政编码和城市。我想我把你的代码放在了好的地方,但也许不是? (玩得开心没问题,这很有趣:))
  • 干得好。乐于助人。
猜你喜欢
  • 2013-07-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
相关资源
最近更新 更多