最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you find the 4 corners of a Google Map V3 when the map is rotated? - Stack Overflow

programmeradmin1浏览0评论

Google maps V3 API has a getBounds method on the map that returns you the Northeast and Southwest corners of the viewport. In theory, I could calculate the Northwest and Southeast corners by using the latitude and longitude of the opposite points, i.e.

NE = (lat= 37.5, lng= -97.5)
SW = (lat= 37.0, lng= -96)
... therefore ...
NW = (lat= 37.5, lng= -96)
SE = (lat= 37.0, lng= -97.5)

(there was even a post about this 5 years ago.)

But all of the answers I've found anywhere online assume the map is pointing due North, meaning "Northeast" really means "top right".

But what happens if the map is rotated? Do I have to use trig based on the orientation of the map to figure out the other two corners? Or does Google give me actual Northeast and Southwest coordinates of an area that enpasses what's in my viewport, even if that means the resulting rectangle has parts that fall outside of the bounds of a rotated map?

Google maps V3 API has a getBounds method on the map that returns you the Northeast and Southwest corners of the viewport. In theory, I could calculate the Northwest and Southeast corners by using the latitude and longitude of the opposite points, i.e.

NE = (lat= 37.5, lng= -97.5)
SW = (lat= 37.0, lng= -96)
... therefore ...
NW = (lat= 37.5, lng= -96)
SE = (lat= 37.0, lng= -97.5)

(there was even a post about this 5 years ago.)

But all of the answers I've found anywhere online assume the map is pointing due North, meaning "Northeast" really means "top right".

But what happens if the map is rotated? Do I have to use trig based on the orientation of the map to figure out the other two corners? Or does Google give me actual Northeast and Southwest coordinates of an area that enpasses what's in my viewport, even if that means the resulting rectangle has parts that fall outside of the bounds of a rotated map?

Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Nov 12, 2015 at 22:21 JonGJonG 1823 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Or does Google give me actual Northeast and Southwest coordinates of an area that enpasses what's in my viewport

That's right, getBounds function returns north-east and south-west corners based on current viewport. The below example illustrates it, in particular it draws the rectangle with the size that enpasses current viewport:

var map;
var area;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 45.518, lng: -122.672},
    zoom: 18,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    //heading: 0,
    //tilt: 45
  });

  google.maps.event.addListenerOnce(map, 'idle', function(){
       drawBounds(map);  
  });
  
}



function drawBounds(map)
{
    var bounds = map.getBounds();
    var areaBounds = {
         north: bounds.getNorthEast().lat(),
         south: bounds.getSouthWest().lat(),
         east: bounds.getNorthEast().lng(),
         west: bounds.getSouthWest().lng()
    };
    
    area = new google.maps.Rectangle({
       strokeColor: '#FF0000',
       strokeOpacity: 0.8,
       strokeWeight: 2,
       fillColor: '#FF0000',
       fillOpacity: 0.35,
       map: map,
       bounds:areaBounds
  });
  console.log(areaBounds);
}


function rotate() {
   var heading = map.getHeading() || 0;
   map.setHeading(heading + 90);
   area.setMap(null);
   drawBounds(map);
}
html, body {
   height: 100%;
   margin: 0;
   padding: 0;
}

#map {
   height: 100%;
}

#floating-panel {
   position: absolute;
   top: 10px;
   left: 25%;
   z-index: 5;
   background-color: #fff;
   padding: 5px;
   border: 1px solid #999;
   text-align: center;
   font-family: 'Roboto','sans-serif';
   line-height: 30px;
   padding-left: 10px;
}
<div id="floating-panel"><input type="button" value="Rotate" onclick="rotate();"></div>
<div id="map"></div>
<script async defer src="https://maps.googleapis./maps/api/js?callback=initMap"></script>

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>