te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Google Maps API: pan to markers from a list. - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps API: pan to markers from a list. - Stack Overflow

programmeradmin3浏览0评论

Hello :) Please bear with me, I'm not a coder but I'm trying to learn by doing. This is the page I'm working on currently; .html. I've currently set up a map using the Google Maps API, using the following javascript:

    function initialize() {

          var mapOptions = {
          zoom: 5,
          center: new google.maps.LatLng(48.160, -6.832),
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP
          };

         map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
             setMarkers(map, cities);
              }


 var cities = [
   ['Groningen', 53.216723950863425, 6.560211181640625, 4],
   ['San Francisco', 34.01131647557699, -118.25599389648437, 5],
   ['New York City', 40.7143528, -74.0059731, 3],
   ['Amsterdam', 52.3702157, 4.8951679, 2],
   ['Maroubra Beach', -33.950198, 151.259302, 1]
 ];

 function setMarkers(map, locations) {

   for (var i = 0; i < locations.length; i++) {
     var city = locations[i];
     var myLatLng = new google.maps.LatLng(city[1], city[2]);
     var marker = new google.maps.Marker({
         position: myLatLng,
         map: map,
         title: city[0],
         zIndex: city[3]
     });
   }
 }

 This continues below...

And then I created a list where every li element, when clicked upon, results in a panning of the map to one of these markers.

I've added the following code and it works very well. But it means that I have to add the longitudes/latitudes for every city in the above code array of "cities", as well as in the code below for the var laLatLng. How can I get the lat's and lon's more easily in the panning script below?

      $(document).ready(function() {
      initialize();

      $("#contacts").on("click", "#sanfransisco", function() {
          var laLatLng = new google.maps.LatLng(34.01131647557699, -118.25599389648437);
          map.panTo(laLatLng);
          map.setZoom(5);
       });
      $("#contacts").on("click", "#groningen", function() {
          var laLatLng = new google.maps.LatLng(53.216723950863425, 6.560211181640625);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#nyc", function() {
          var laLatLng = new google.maps.LatLng(40.7143528, -74.0059731);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#losangeles", function() {
          var laLatLng = new google.maps.LatLng(52.3702157, 4.8951679);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
  });

I hope someone can explain to me how to get the variables from cities into clickable list javascript. Thanks a lot, I deeply appreciate your response!

Hello :) Please bear with me, I'm not a coder but I'm trying to learn by doing. This is the page I'm working on currently; http://www.websu.it/devnw/dev/contact.html. I've currently set up a map using the Google Maps API, using the following javascript:

    function initialize() {

          var mapOptions = {
          zoom: 5,
          center: new google.maps.LatLng(48.160, -6.832),
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP
          };

         map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
             setMarkers(map, cities);
              }


 var cities = [
   ['Groningen', 53.216723950863425, 6.560211181640625, 4],
   ['San Francisco', 34.01131647557699, -118.25599389648437, 5],
   ['New York City', 40.7143528, -74.0059731, 3],
   ['Amsterdam', 52.3702157, 4.8951679, 2],
   ['Maroubra Beach', -33.950198, 151.259302, 1]
 ];

 function setMarkers(map, locations) {

   for (var i = 0; i < locations.length; i++) {
     var city = locations[i];
     var myLatLng = new google.maps.LatLng(city[1], city[2]);
     var marker = new google.maps.Marker({
         position: myLatLng,
         map: map,
         title: city[0],
         zIndex: city[3]
     });
   }
 }

 This continues below...

And then I created a list where every li element, when clicked upon, results in a panning of the map to one of these markers.

I've added the following code and it works very well. But it means that I have to add the longitudes/latitudes for every city in the above code array of "cities", as well as in the code below for the var laLatLng. How can I get the lat's and lon's more easily in the panning script below?

      $(document).ready(function() {
      initialize();

      $("#contacts").on("click", "#sanfransisco", function() {
          var laLatLng = new google.maps.LatLng(34.01131647557699, -118.25599389648437);
          map.panTo(laLatLng);
          map.setZoom(5);
       });
      $("#contacts").on("click", "#groningen", function() {
          var laLatLng = new google.maps.LatLng(53.216723950863425, 6.560211181640625);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#nyc", function() {
          var laLatLng = new google.maps.LatLng(40.7143528, -74.0059731);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
      $("#contacts").on("click", "#losangeles", function() {
          var laLatLng = new google.maps.LatLng(52.3702157, 4.8951679);
          map.panTo(laLatLng);
          map.setZoom(6);
      });
  });

I hope someone can explain to me how to get the variables from cities into clickable list javascript. Thanks a lot, I deeply appreciate your response!

Share Improve this question edited Jan 13, 2013 at 17:38 Robin Papa asked Jan 13, 2013 at 17:23 Robin PapaRobin Papa 1801 gold badge2 silver badges11 bronze badges 2
  • 1 you want to fetch the lat/long of various cities based on the city name? – doublesharp Commented Jan 13, 2013 at 17:25
  • Yes, that would be ideal :) – Robin Papa Commented Jan 13, 2013 at 17:28
Add a ment  | 

2 Answers 2

Reset to default 6

If you convert the cities array to an object like:

var cities = {
  'Groningen':  [ 53.216723950863425, 6.560211181640625, 4],
   'San Francisco': [ 34.01131647557699, -118.25599389648437, 5],
   'New York City': [ 40.7143528, -74.0059731, 3]

};

And add a data- attribute with matching city name along with a mon class name to each of the links:

/* not sure what html looks like , using pseudo "tagName"*/
<tagName class="map_link" data-city="Groningen">Groningen</tagName>

You can create one handler for the whole class:

$("#contacts").on("click", ".map_link", function() {
          /* "this" within handler is the actual element clciked*/

          /* find correct array from "cities" object */
         var data=cities[ $(this).data('city') ]; /* returns array[ 53.21, 6.56, 4]*/
          var laLatLng = new google.maps.LatLng( data[0],  data[1]);
          map.panTo(laLatLng);
          map.setZoom( data[2]);
       });

Here's how I'd do it, simply using the reverse geocoding capabilities of Google Maps to lookup the city names:

<ul id="cities">
  <li>San Fransisco</li>
  <li>Groningen</li>
  <li>New York City</li>
  <li>Los Angeles</li>
</ul>

JS

$('#cities li').on('click', function() {
  $.ajax({
    url: 'http://maps.googleapis./maps/api/geocode/json?address='+decodeURIComponent( $(this).text() )+'&sensor=false',
    type: 'GET'
  }).done(function(data) {
    var laLatLng = new google.maps.LatLng(data.results[0].geometry.location.lat, data.results[0].geometry.location.lng);
    map.panTo(laLatLng);
    map.setZoom(5);
  });
});

FIDDLE

发布评论

评论列表(0)

  1. 暂无评论