地址到谷歌地图上的数据库

Address into database on google map

本文关键字:数据库 谷歌地图 地址      更新时间:2023-09-26

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

我还有一个函数地理编码器,可以将此地址更改为 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); 
 ?>
首先,

您可以使用 AJAX 请求(建议为此使用 jQuery)对服务器上的 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有一般的理解,你应该能够实现你的目标。