PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用PHP,MySQL和Google Maps创建商店定位器有点不同大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我想用Google Maps创建一个商店定位器.

我有一个数据库,其中包含一个旅游对象表(及其坐标)和一个旅馆表(及其坐标).

我希望用户在加载伦敦塔大厦的页面后,可以看到在10公里半径内对象附近的酒店,并在带有标记的Google Maps上显示结果.

到目前为止,我只设法从数据库获取具有Haversin公式的10公里范围内的酒店,并将其显示为文本:

$result = MysqL_query("SELECT  nume,  poze, descriere, link, (
(
    ACOS( SIN( 45.515038 * PI( ) /180 ) * SIN( latitudine * PI( ) /180 ) +
          COS( 45.515038 * PI( ) /180 ) * COS( latitudine * PI( ) /180 ) *
          COS( ( 25.366935 - longitudine ) * PI( ) /180 )
    ) *180 / PI( )
) *60 * 1.1515 * 1.609344
) AS distance
FROM `unitati`
HAVING distance <= '10'
ORDER BY distance ASC
LIMIT 0 , 30");

如何在地图上将它们显示标记

发现了这一点,并认为它可以为我提供帮助:http://code.google.com/intl/ro-RO/apis/maps/articles/phpsqlsearch.html,但是它有一个不同的逻辑:用户输入地址.

解决方法:

此示例接受您的查询(并且您需要为每个目标使用lat / lng编辑45.515038和25.366935),并将其输出为JS数组数组(如果愿意,可以使其更正式的JSON)

然后,它遍历该数组,为每个数组创建标记并将它们放置在地图上.最后,它向每个添加一个单击侦听器,以便显示相关信息.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<style>
#map_canvas{width:500px;height:500px;}
</style>
</head>

<body>
<div id="map_canvas"></div>
<script>
<?PHP
//you'll first need to connect to your db
//you also have to edit the lat/lng in your SELECT statement to be that of your objective
$result = MysqL_query("SELECT  latitudine, longitudine, nume,  poze, descriere, link, (
(
    ACOS( SIN( 45.515038 * PI( ) /180 ) * SIN( latitudine * PI( ) /180 ) +
          COS( 45.515038 * PI( ) /180 ) * COS( latitudine * PI( ) /180 ) *
          COS( ( 25.366935 - longitudine ) * PI( ) /180 )
    ) *180 / PI( )
) *60 * 1.1515 * 1.609344
) AS distance
FROM `unitati`
HAVING distance <= '10'
ORDER BY distance ASC
LIMIT 0 , 30");
$c=0;
echo "var data=[";
while($markers=MysqL_fetch_array($result)){
    if($c>0){echo ",";}
    echo "['".$markers[0]."','".$markers[1]."','".$markers[2]."','".$markers[3]."','".$markers[4]."','".$markers[5]."','".$markers[6]."'"."]";
    $c++;
}
echo "];";
?>
    //var data=[['45','-73','home','poz1','desc1','link1'],['43','-75','work','poz2','desc2','link2']];
    var places=new Array();
    var map;
    var MyInfoWindow = new google.maps.InfoWindow({Content: 'Loading...'});
    var bounds = new google.maps.LatLngBounds();
    var myOptions = {
        zoom: 9,
        mapTypeControl: false,
        mapTypEID: google.maps.MapTypEID.roaDMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);
    for(i=0;i<data.length;i++){
        var point=new google.maps.LatLng(data[i][0],data[i][1]);
        var a = new google.maps.Marker({position: point,map: map, icon:'someHotelIcon.png'});
        a.lat=data[i][0];
        a.lng=data[i][1];
        a.nume=data[i][2];
        a.poze=data[i][3];
        a.desc=data[i][4];
        a.url=data[i][5];
        places.push(a);
    }
    for(i=0;i<places.length;i++){
        var point2=new google.maps.LatLng(places[i].lat,places[i].lng);
        bounds.extend(point2);
    }
    map.fitBounds(bounds);
    var objLoc=new google.maps.LatLng(45.515038,26.366935);
    var objectiveMarker = new google.maps.Marker({position: objLoc,map: map, icon:'objectiveIcon.png'});    //---------------Marker for objective
    for (var i = 0; i < places.length; i++) {
        var marker = places[i];
        google.maps.event.addListener(marker, 'click', function () {
        MyInfoWindow.setContent(this.nume+'<br/>'+this.desc+'<br/><a href=\"'+thiS.Url+'\">link</a>');
        MyInfoWindow.open(map, this);
        });
    }
</script>
</body>
</html>

大佬总结

以上是大佬教程为你收集整理的使用PHP,MySQL和Google Maps创建商店定位器有点不同全部内容,希望文章能够帮你解决使用PHP,MySQL和Google Maps创建商店定位器有点不同所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:使用PHP