��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - Google Maps v3: need multiple draggable markers to update HTML input fields - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps v3: need multiple draggable markers to update HTML input fields - Stack Overflow

programmeradmin1浏览0评论

I'm working on a project using Google Maps v3 that will allow users to drag and drop randomly placed markers (quantity, coordinates, and labels generated with php).

I would like the latitude and longitude of moved markers to update html input fields on the page. Unfortunately, I do not know enough about js to give each marker a unique identity in an efficient way by using arrays and/or 'variable variables'.

Here is my code thus far:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
function initialize() 
{
    var latlng = new google.maps.LatLng(39.3939,-119.861);
    var myOptions = 
    {
      zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    // Set Border Coords
    var BorderCoords = 
    [
        new google.maps.LatLng(39.3952726888,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.86277152),
        new google.maps.LatLng(39.3952726888,-119.86277152)   
    ];      

    // Define Border
    Border = new google.maps.Polygon
    ({
        map: map,
        paths: BorderCoords,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 2
    });

    // Set marker coords
    var MarkerCoords =
    [
        [39.3930761,-119.8612324],
        [39.3947606,-119.8617452],
        [39.3948516,-119.8619689],
        [39.3929412,-119.859542],
        [39.3947902,-119.8601571],
        [39.3940501,-119.8593369]
    ];

    // Set marker properties
    var MarkerProp = 
    [
        ["Red","A"],
        ["Red","B"],
        ["Red","C"],
        ["Blue","A"],
        ["Blue","B"],
        ["Blue","C"]
    ];

    // Define marker coords
    for (var i = 0; i < MarkerCoords.length; i++)
    {
        var markers = new google.maps.Marker
        ({
            map: map,
            position: new google.maps.LatLng(MarkerCoords[i][0],MarkerCoords[i][1]),
            clickable: true,
            draggable: true,
            icon: 'img/icons/'+MarkerProp[i][0]+'/letter_'+MarkerProp[i][1]+'.png'
        });

        google.maps.event.addListener(markers, "dragend", function() 
        {
            //These variables will not work
            //var latstr = eval('lat_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lngstr = eval('lng_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lat = document.getElementById(latstr);
            //var lng = document.getElementById(lngstr);

            //these lat/lng variables work for only one set of input fields
            var lat = document.getElementById('lat_Blue_C');
            var lng = document.getElementById('lng_Blue_C');
            var coords = this.getPosition()
            lat.value = coords.lat();
            lng.value = coords.lng();
        });
    }
}
</script>

</head>
<body onload="initialize()">
<div id="map_canvas" style="width:700px; height:400px"></div>
<br/>
<input id="lat_Red_A" type="text">
<input id="lng_Red_A" type="text">
<br/>
<input id="lat_Red_B" type="text">
<input id="lng_Red_B" type="text">
<br/>
<input id="lat_Red_C" type="text">
<input id="lng_Red_C" type="text">
<br/>
<input id="lat_Blue_A" type="text">
<input id="lng_Blue_A" type="text">
<br/>
<input id="lat_Blue_B" type="text">
<input id="lng_Blue_B" type="text">
<br/>
<input id="lat_Blue_C" type="text">
<input id="lng_Blue_C" type="text">
<br/>
</body>

</html>

I'm working on a project using Google Maps v3 that will allow users to drag and drop randomly placed markers (quantity, coordinates, and labels generated with php).

I would like the latitude and longitude of moved markers to update html input fields on the page. Unfortunately, I do not know enough about js to give each marker a unique identity in an efficient way by using arrays and/or 'variable variables'.

Here is my code thus far:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google./maps/api/js?sensor=true"></script>
<script type="text/javascript">
function initialize() 
{
    var latlng = new google.maps.LatLng(39.3939,-119.861);
    var myOptions = 
    {
      zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    // Set Border Coords
    var BorderCoords = 
    [
        new google.maps.LatLng(39.3952726888,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.86277152),
        new google.maps.LatLng(39.3952726888,-119.86277152)   
    ];      

    // Define Border
    Border = new google.maps.Polygon
    ({
        map: map,
        paths: BorderCoords,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 2
    });

    // Set marker coords
    var MarkerCoords =
    [
        [39.3930761,-119.8612324],
        [39.3947606,-119.8617452],
        [39.3948516,-119.8619689],
        [39.3929412,-119.859542],
        [39.3947902,-119.8601571],
        [39.3940501,-119.8593369]
    ];

    // Set marker properties
    var MarkerProp = 
    [
        ["Red","A"],
        ["Red","B"],
        ["Red","C"],
        ["Blue","A"],
        ["Blue","B"],
        ["Blue","C"]
    ];

    // Define marker coords
    for (var i = 0; i < MarkerCoords.length; i++)
    {
        var markers = new google.maps.Marker
        ({
            map: map,
            position: new google.maps.LatLng(MarkerCoords[i][0],MarkerCoords[i][1]),
            clickable: true,
            draggable: true,
            icon: 'img/icons/'+MarkerProp[i][0]+'/letter_'+MarkerProp[i][1]+'.png'
        });

        google.maps.event.addListener(markers, "dragend", function() 
        {
            //These variables will not work
            //var latstr = eval('lat_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lngstr = eval('lng_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lat = document.getElementById(latstr);
            //var lng = document.getElementById(lngstr);

            //these lat/lng variables work for only one set of input fields
            var lat = document.getElementById('lat_Blue_C');
            var lng = document.getElementById('lng_Blue_C');
            var coords = this.getPosition()
            lat.value = coords.lat();
            lng.value = coords.lng();
        });
    }
}
</script>

</head>
<body onload="initialize()">
<div id="map_canvas" style="width:700px; height:400px"></div>
<br/>
<input id="lat_Red_A" type="text">
<input id="lng_Red_A" type="text">
<br/>
<input id="lat_Red_B" type="text">
<input id="lng_Red_B" type="text">
<br/>
<input id="lat_Red_C" type="text">
<input id="lng_Red_C" type="text">
<br/>
<input id="lat_Blue_A" type="text">
<input id="lng_Blue_A" type="text">
<br/>
<input id="lat_Blue_B" type="text">
<input id="lng_Blue_B" type="text">
<br/>
<input id="lat_Blue_C" type="text">
<input id="lng_Blue_C" type="text">
<br/>
</body>

</html>
Share Improve this question asked Jun 28, 2011 at 17:42 NeonDevilNeonDevil 1061 silver badge9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Here is the answer. Define a custom id based on your marker properties and based on that access yout input field ids:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google./maps/api/js?sensor=true"></script>
<script type="text/javascript">
function initialize() 
{
    var latlng = new google.maps.LatLng(39.3939,-119.861);
    var myOptions = 
    {
      zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    // Set Border Coords
    var BorderCoords = 
    [
        new google.maps.LatLng(39.3952726888,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.85922848),
        new google.maps.LatLng(39.3925273112,-119.86277152),
        new google.maps.LatLng(39.3952726888,-119.86277152)   
    ];      

    // Define Border
    Border = new google.maps.Polygon
    ({
        map: map,
        paths: BorderCoords,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 2
    });

    // Set marker coords
    var MarkerCoords =
    [
        [39.3930761,-119.8612324],
        [39.3947606,-119.8617452],
        [39.3948516,-119.8619689],
        [39.3929412,-119.859542],
        [39.3947902,-119.8601571],
        [39.3940501,-119.8593369]
    ];

    // Set marker properties
    var MarkerProp = 
    [
        ["Red","A",],
        ["Red","B"],
        ["Red","C"],
        ["Blue","A"],
        ["Blue","B"],
        ["Blue","C"]
    ];

    // Define marker coords
    for (var i = 0; i < MarkerCoords.length; i++)
    {
        var markers = new google.maps.Marker
        ({
            map: map,
            position: new google.maps.LatLng(MarkerCoords[i][0],MarkerCoords[i][1]),
            clickable: true,
            draggable: true,

            //define a custom id based on marker properties

            my_id: MarkerProp[i][0] +"_"+MarkerProp[i][1],
            icon: 'img/icons/'+MarkerProp[i][0]+'/letter_'+MarkerProp[i][1]+'.png'
        });

        google.maps.event.addListener(markers, "dragend", function() 
        {
            //These variables will not work
            //var latstr = eval('lat_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lngstr = eval('lng_'+MarkerProp[i][0]+'_'+MarkerProp[i][1]);
            //var lat = document.getElementById(latstr);
            //var lng = document.getElementById(lngstr);

//get the id of the marker
var marker_id = this.my_id;



            //match the fields to update
            var lat = document.getElementById('lat_' + marker_id);
            var lng = document.getElementById('lng_' + marker_id);
            var coords = this.getPosition()
            lat.value = coords.lat();
            lng.value = coords.lng();
        });
    }
}
</script>

</head>
<body onload="initialize()">
<div id="map_canvas" style="width:700px; height:400px"></div>
<br/>
<input id="lat_Red_A" type="text">
<input id="lng_Red_A" type="text">
<br/>
<input id="lat_Red_B" type="text">
<input id="lng_Red_B" type="text">
<br/>
<input id="lat_Red_C" type="text">
<input id="lng_Red_C" type="text">
<br/>
<input id="lat_Blue_A" type="text">
<input id="lng_Blue_A" type="text">
<br/>
<input id="lat_Blue_B" type="text">
<input id="lng_Blue_B" type="text">
<br/>
<input id="lat_Blue_C" type="text">
<input id="lng_Blue_C" type="text">
<br/>
</body>

</html>

Well one of the beautiful things about JavaScript is that you can assign objects fields on the fly. One thing you could do would give each marker a name or an id. To do that you simply say:

marker.id = whatever;

Then you can get the value of that like you would any property by calling marker.id;

That is probably the most efficient way of giving them unique id's. In your eventhandler for the dragend you could then just do and if-then-else to check which marker it was and update the related input fields.

发布评论

评论列表(0)

  1. 暂无评论