使用PHP将城市名称传递给Google Maps

时间:2018-10-18 06:46:39

标签: javascript php google-maps

我正在尝试打开显示将显示当前城市名称标记的显示,每次单击都会显示该错误,但是那里出了点问题,我确实尝试这样做但没有运气。我尝试了几种方法来从输入中获取价值(没有成功)。然后,我尝试通过onclick获取显示的值,再次无法正常工作。我知道我需要在javascript中声明address变量,但是我坚持了这一点。 请检查我的代码。

HTML部分。在这里,我打开显示面板以显示地图,然后尝试将值city_name发送到JavaScript。

<button data-open="user-location" id="submit" class="clear primary button" onclick="geocodeAddress(<?php echo $cities[($product['product_city_id'] - 0507)]['city_name']; ?>); "><i class="fas fa-map-marker-alt"></i> Map</button>

<div id="user-location" class="reveal-modal" data-reveal   aria-labelledby="modalTitle" aria-hidden="true" role="dialog" data-reset-on-close="true" >
    <div id="map"></div>
    <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</div>


function dropdownMenu() {
    var x = document.getElementById("dropdownClick");
    if (x.className === "topnav") {
        x.className += " responsive";
        /*change topnav to topnav.responsive*/
    } else {
        x.className = "topnav";
    }
}

基于Google Maps的JavaScript部分。

function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        "zoom": 8,
        "center": { "lat": -34.397, "lng": 150.644 }
    });
    var geocoder = new google.maps.Geocoder();

    document.getElementById('submit').addEventListener('click', function() {
        geocodeAddress(geocoder, map);
    });
}

function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({ "address": address}, function(results, status) {
        if (status === 'OK') {
            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                "map": resultsMap,
                "position": results[0].geometry.location
            });
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

0 个答案:

没有答案