谷歌地图搜索位置中心地图问题在PHP

时间:2018-04-02 04:55:27

标签: javascript php mysql google-maps

我正在研究PHP,我已经从数据库创建了基于地址的谷歌地图,在我的网站主页上有一个搜索位置自动填充输入类型,如果我正在搜索(BTM layout Bangalore)它正在显示地图的位置我需要当前搜索地图中心位置的地图请帮助某人。

index.php

<div class="form-group keyword">
    <input id="locationTextField" name= "area_name" type="text" size="50">   
    <button type="submit" name="filter"><img src="assets/images/search.png" alt="Rentozy" /></button>
</div>

这里映射脚本:pglist.php。

mysql查询:

    <?php
    if(isset($_POST['area_name'])){
        $location_type =$_POST['area_name'];
        $arr = explode(' ',trim($location_type));
        $listing=$arr[0];
        $all_location ="select * from tbl_master_property where pg_address like '%$listing%'";
        $location_result=$conn->query($all_location);
        while($row=mysqli_fetch_assoc($location_result)){
            //$location_result1[] = $row;
            $men_projects[] = $row;
            $locations[] = $row['pg_address'];
        }
    }
    $locations = json_encode($locations);
?>

谷歌地图脚本:

<div class="map">
    <div id="map"></div>
</div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyBAM5Cs2VsrOBs8Idqy0t0o6vw4hEU0Lys"></script> 
<script type="text/javascript">
    var delay = 100;
    var infowindow = new google.maps.InfoWindow();
    var latlng = new google.maps.LatLng(12.9716, 77.5946);
    var mapOptions = {
        zoom: 13,
        center: latlng,
    }
    var geocoder = new google.maps.Geocoder(); 
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);
    var bounds = new google.maps.LatLngBounds();
    function geocodeAddress(address, next) {
        geocoder.geocode({address:address}, function (results,status){
            if (status == google.maps.GeocoderStatus.OK) {
                var p = results[0].geometry.location;
                var lat=p.lat();
                var lng=p.lng();
                createMarker(address,lat,lng);
            }
            else {
                if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                    nextAddress--;
                    delay++;
                } else {}
            next();
        });
    }
    function createMarker(add,lat,lng) {
        var contentString = add;
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat,lng),
            animation: google.maps.Animation.DROP,
            draggable: true,
            map: map,
        });
        marker.addListener('click', toggleBounce);
        function toggleBounce() {
            if (marker.getAnimation() !== null) {
                marker.setAnimation(null);
            } else {
                marker.setAnimation(google.maps.Animation.BOUNCE);
            }
        }
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(contentString); 
            infowindow.open(map,marker);
        });
        bounds.extend(marker.position);
    }
    var locations = <?= $locations ?>;
    console.log(locations);
    var nextAddress = 0;
    function theNext() {
        if (nextAddress < locations.length) {
            setTimeout('geocodeAddress("'+locations[nextAddress]+'",theNext)', delay);
            nextAddress++;
        } else {
            map.fitBounds(bounds);
        }
    }
    theNext();
</script>

0 个答案:

没有答案