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 - Maps API v3: Calculate bearing - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Maps API v3: Calculate bearing - Stack Overflow

programmeradmin3浏览0评论

I've been struggling with this for a while & need some advice. I have seen some similar topics but can't find an answer that works for me.

I am creating an app using google maps API that will determine your location, determine a specific point (point b) and then move an arrow to point in the direction of point b. I am having trouble calculating the bearing, could anyone offer some assistance?

I have: - the lat/lng of point a - the lat/lng of point b - the device knows where North is and I am able to calculate how many degrees away from north the device is pointing

I need: a calculation that will take this information and spit out a number of degrees to point the arrow.

I have looked here which seems useful but I still can't get it working - it's returning NaN

.html

I'm not really a js pro so would really appreciate some guidance here.

Thanks very much!

I've been struggling with this for a while & need some advice. I have seen some similar topics but can't find an answer that works for me.

I am creating an app using google maps API that will determine your location, determine a specific point (point b) and then move an arrow to point in the direction of point b. I am having trouble calculating the bearing, could anyone offer some assistance?

I have: - the lat/lng of point a - the lat/lng of point b - the device knows where North is and I am able to calculate how many degrees away from north the device is pointing

I need: a calculation that will take this information and spit out a number of degrees to point the arrow.

I have looked here which seems useful but I still can't get it working - it's returning NaN

http://www.movable-type.co.uk/scripts/latlong.html

I'm not really a js pro so would really appreciate some guidance here.

Thanks very much!

Share Improve this question edited May 26, 2014 at 7:21 MrUpsidown 22.5k15 gold badges82 silver badges140 bronze badges asked May 26, 2014 at 6:08 user3675220user3675220 811 silver badge4 bronze badges 1
  • 2 can you share your attempt (that returned NaN)? – sfletche Commented May 26, 2014 at 6:12
Add a ment  | 

2 Answers 2

Reset to default 14

You can use the Geometry Library.

You need to add it to the API call:

https://maps.googleapis./maps/api/js?libraries=geometry

Then use the puteHeading method:

var heading = google.maps.geometry.spherical.puteHeading(pointA, pointB);

where pointA and pointB are the two LatLng objects.

https://developers.google./maps/documentation/javascript/reference?csw=1#spherical

Below is a dynamically working example.

<!DOCTYPE html>
<html>
<head>
<style>
    html,
    body,
    #map-canvas {
        height: 100%;
        width: 100%;
        margin: 0px;
        padding: 0px;
    }
</style>
</head>
<body>
<script src="https://maps.googleapis./maps/api/js?sensor=false&libraries=geometry&ext=.js"></script>
<div id="info"></div>
<div id="map-canvas"></div>
<script type="text/javascript">
    var map;
    var infowindow = new google.maps.InfoWindow();
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(32.0748, 34.774);
        var mapOptions = {
            zoom: 15,
            center: latlng
        }
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        point1 = new google.maps.Marker({
            map: map,
            draggable: true,
            position: google.maps.geometry.spherical.puteOffset(map.getCenter(), 0, 80)
        });
        google.maps.event.addListener(point1, 'dragend', bisectAngle);

        point2 = new google.maps.Marker({
            map: map,
            draggable: true,
            position: google.maps.geometry.spherical.puteOffset(map.getCenter(), 800, -58.6)
        });
        google.maps.event.addListener(point2, 'dragend', bisectAngle);

        var poly3 = new google.maps.Polyline({
            path: [point1.getPosition(), point2.getPosition()],
            map: map
        });
        poly3.binder = new MVCArrayBinder(poly3.getPath());
        point1.bindTo('position', poly3.binder, '0'); //this makes the line bind to point 1
        point2.bindTo('position', poly3.binder, '1'); //and point 2

        var bounds = new google.maps.LatLngBounds();
        bounds.extend(point1); 
        bounds.extend(point2);
        map.fitBounds(bounds); //zooms in
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    google.maps.event.addDomListener(window, 'load', bisectAngle);

    function bisectAngle() {
        var head1 = google.maps.geometry.spherical.puteHeading(point1.getPosition(), point2.getPosition());
        document.getElementById('info').innerHTML = "Bearing is " + head1.toFixed(1) + " or " + (head1 + 90).toFixed(1) + " if E-W is 0. Better try daniel street...";
    }

    /*
     * Use bindTo to allow dynamic drag of markers to refresh poly.
     */
    function MVCArrayBinder(mvcArray) { //credit to https://stackoverflow./a/26663570/2381899
        this.array_ = mvcArray;
    }
    MVCArrayBinder.prototype = new google.maps.MVCObject();
    MVCArrayBinder.prototype.get = function (key) {
        if (!isNaN(parseInt(key))) {
            return this.array_.getAt(parseInt(key));
        } else {
            this.array_.get(key);
        }
    }
    MVCArrayBinder.prototype.set = function (key, val) {
        if (!isNaN(parseInt(key))) {
            this.array_.setAt(parseInt(key), val);
        } else {
            this.array_.set(key, val);
        }
    }
</script>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论