首页 > 解决方案 > PHP获取地图位置当前街道地址不出现

问题描述

我正在使用脚本来获取纬度和经度,以便在加载页面后立即显示在地图中。地图出现,但它的位置显示为空,名称中只有一个逗号 (,),而不是当前位置。没有出现该街道的名称。标记出现在世界地图的中间。

它适用于具有固定数字的位置,但它必须适用于当前位置。

代码:

<div class="my-coloapse-body open"  id="my-coloapse-1" >
    <div class="map-section" >
        <div class="input-streename-row">
            <input id="streetname" type="text" name="name" class="" placeholder="όνομα δρόμου">
            <input name='longitude' id='longitude' type='hidden'/>
            <input name='latitude' id='latitude' type='hidden'/>

            <span class="relocate  ti-target " onclick="getMapLocation();"></span>
        </div>
        <div id="map"> </div>

        <?php $ITEMS = array(); //get_query("SELECT * FROM `pillars`"); ?> 

        <?php
        if (!filter_input(INPUT_GET, 'map-provider') == 'osm') {
            include("pillar-position-google.php");
        } else {
            include("pillar-position-osm.php");
        }
        $NOMALOCATION = 1;
        include(__DIR__ . "/../position-common.php");
        ?>
        <script>

            getMap('<?php echo $PILLAR["latitude"]; ?>', '<?php echo $PILLAR["longitude"]; ?>');

        </script>
    </div>
</div>

函数getmap:

// 使用坐标获取地图

function getMap(latitude, longitude) {
    var mapOptions = {
        center: new google.maps.LatLng(0, 0),
        zoom: 20,
        maxZoom: 20,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: [{
                featureType: "poi",
                elementType: "labels",
                stylers: [{ visibility: "off" }]
            },
            { "featureType": "all", "elementType": "all", "stylers": [{ "saturation": -100 }, { "gamma": 0.5 }] }
        ],
        mapTypeControl: true,
        streetViewControl: false
    };
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    locations.forEach(function(location) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(location["lat"], location["lng"]),
            id: location["id"],
            icon: location["icon"],
            map: map
        });
        google.maps.event.addListener(marker, 'click', function(evt) {
            $.fancybox.open({
                src: '#dialogContent-' + this.id,
                type: 'inline',
                opts: {
                    afterShow: function(instance, current) {
                        console.info('done!');
                    }
                }
            });
            markers.push(marker);
        });
    });
    var latLong = new google.maps.LatLng(latitude, longitude);
    var marker = new google.maps.Marker({
        position: latLong,
        draggable: true,
    });
    marker.setMap(map);
    map.setZoom(20);
    map.setCenter(marker.getPosition());
    google.maps.event.addListener(marker, 'dragend', function() {
        $("#streetname").val("...");
        $.ajax({
            type: 'POST',
            url: '<?php echo APP_URL ?>get_address.php',
            data: {
                lat: marker.getPosition().lat,
                lng: marker.getPosition().lng,
            },
            success: function(data) {
                $("#streetname").val(data.trim());
            }
        });
        $("#latitude").val(marker.getPosition().lat);
        $("#longitude").val(marker.getPosition().lng);
    });
    $("#streetname").val("...");
    $.ajax({
        type: 'POST',
        url: '<?php echo APP_URL ?>get_address.php',
        data: {
            lat: marker.getPosition().lat,
            lng: marker.getPosition().lng,
        },
        success: function(data) {
            $("#streetname").val(data.trim());
        }
    });
    $("#latitude").val(marker.getPosition().lat);
    $("#longitude").val(marker.getPosition().lng);
}

编辑:

该页面似乎运行了 2 个脚本。首先它采用地图上的默认位置(0,0),如果我编辑一个固定位置,它会在当前位置几秒钟后刷新(例如雅典的纬度和经度)

function watchMapPosition() {

    return navigator.geolocation.watchPosition
    (onMapWatchSuccess, onMapError, { enableHighAccuracy: true });
}




// watchMapPosition();

getMapLocation();
</script>


    <script>
//  getMap( '37.94767073344734','23.734566048514125 ');
  </script>

不显示名字

标签: php

解决方案


推荐阅读