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

Google Maps Javascript API - bounds.contains method - Stack Overflow

programmeradmin2浏览0评论

I created a map with the Google Maps API (Javascript) as follows:

var currentLocation = {lat: 47.3663615, lng: 8.5388539};
var map = new google.maps.Map(document.getElementById('map'), {
    zoom: zoomLevel,
    center: currentLocation,
    mapTypeId: google.maps.MapTypeId.TERRAIN
});

I was then trying to use the bound.contain function to check whether various LatLng positions are contained within the map bounds or not:

var center = map.getCenter(); // works fine
var bounds = map.getBounds(); // works fine
var x = bounds.contains(center); // works fine, delivers true
var a = bounds.contains(currentLocation); throws an error (Uncaught TypeError: a.lat is not a function)

After some debugging, it seems that the currentLocation Object doesn't have the right format to be processed with the method "contains". Please note that I'm quite new to javascript and the Google Maps api.

Thanks in advance for your help.

I created a map with the Google Maps API (Javascript) as follows:

var currentLocation = {lat: 47.3663615, lng: 8.5388539};
var map = new google.maps.Map(document.getElementById('map'), {
    zoom: zoomLevel,
    center: currentLocation,
    mapTypeId: google.maps.MapTypeId.TERRAIN
});

I was then trying to use the bound.contain function to check whether various LatLng positions are contained within the map bounds or not:

var center = map.getCenter(); // works fine
var bounds = map.getBounds(); // works fine
var x = bounds.contains(center); // works fine, delivers true
var a = bounds.contains(currentLocation); throws an error (Uncaught TypeError: a.lat is not a function)

After some debugging, it seems that the currentLocation Object doesn't have the right format to be processed with the method "contains". Please note that I'm quite new to javascript and the Google Maps api.

Thanks in advance for your help.

Share Improve this question asked Feb 21, 2016 at 22:18 petepete 211 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

LatLngBounds.contains currently requires a LatLng as argument, a LatLngBoundsLiteral will not be accepted.

Use

var currentLocation = new google.maps.LatLng(47.3663615,8.5388539);
发布评论

评论列表(0)

  1. 暂无评论