- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Google Maps API 构建了一个 map ,该 map 将根据 'whereNew.js' 中的纬度和经度绘制点,其颜色将取决于 'MarketData.js' 中的另一个参数。单击该标记时,它将显示一个信息窗口,其中包含“显示路线”按钮和一些基于“MarketData.js”的信息。至此代码已成功执行。
此后,当单击任何标记中的“显示路线”按钮时,我希望 Google Maps API 显示从“MarketData.js”第 8 列的每个位置到该标记的所有路线。但是,当我单击该按钮,出现此错误:
Uncaught TypeError: Cannot read property 'route' of undefined at calculateAndDisplayRoute
whereNewTest1.html:162
我不明白我做错了什么。请帮忙
whereNewTest1.html
<html>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Markets of Kolkata</title>
<style>
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#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;
}
#warnings-panel {
width: 100%;
height: 10%;
text-align: center;
}
</style>
<link href="http://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<!-- If you are in China, you may need to use theis site for the Google Maps code
<script src="http://maps.google.cn/maps/api/js" type="text/javascript"></script> -->
<!--<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> -->
<!-- <script async defer src=
"https://maps.googleapis.com/maps/api/js?libraries=geometry,drawing&key=AIzaSyDrdSH53E0MRIfajUGZQiFHAB0aLfzFIVU&v=3&v=3&callback=displayDirections"></script>
</script> -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBbXta5ppMyzPmTCGNsyP-djMlSOGJ9t9o&callback=initialize">
</script>
<script src="whereNew.js"></script>
<script src="marketData.js"></script>
<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var markerArray = [];
var dest ;
function initialize()
{
//alert("To see the title of a marker, hover over the marker but don't click.");
var myLatlng = new google.maps.LatLng(22.39361,88.099263)
var mapOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var markerArray = [];
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer(
{
map: map
});
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
i = 0;
var markers = [];
for ( pos in myData )
{
var row = myData[pos];
var row1 = MarketInfo[i];
window.console && console.log(row) && console.log(row1);
// if ( i < 3 ) { alert(row); }
var newLatlng = new google.maps.LatLng(row[0], row[1]);
var marker = new google.maps.Marker(
{
position: newLatlng,
map: map,
title: row[3],
});
var infowindow = new google.maps.InfoWindow({ });
var largeInfowindow = new google.maps.InfoWindow({ });
dest = row1[1];
var content = row1[2] + '<br/><button onclick = "calculateAndDisplayRoute(directionsDisplay, directionsService, markerArray, map, dest);">Show Routes</button>';
if((content.search("WHOLESALE"))>-1)
{
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png');
}
else if((content.search("WHOLE SALE"))>-1)
{
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png');
}
else if((content.search("MAJOR"))>-1)
{
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
}
else
{
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
}
google.maps.event.addListener(marker,'click', (function(marker,content,infowindow)
{
return function()
{
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));
markers.push(marker);
i = i + 1;
}
function populateInfoWindow(marker, infowindow)
{
if(infowindow.marker != marker)
{
infowindow.marker = marker;
infowindow.setContent('<div>'+infowindow.content+'</div>');
infowindow.open(map, marker);
infowindow.addListener('closeclick', function(){
infowindow.setMarker(null);
});
}
}
}
function calculateAndDisplayRoute(directionsDisplay, directionsService, markerArray, map, dest)
{
for (var i = 0; i < markerArray.length; i++)
{
markerArray[i].setMap(null);
}
for (pos in MarketInfo)
{
var row = MarketInfo[pos];
// Second loop for multiple destination
var str_array = row[7].split(',');
for (var j = 0; j < str_array.length; j++)
{
directionsService.route(
{
origin: str_array[j],
destination: dest,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK')
{
document.getElementById('warnings-panel').innerHTML = '<b>' + response.routes[0].warnings + '</b>';
var dirDisplay = new google.maps.DirectionsRenderer({
map: map
});
dirDisplay.setDirections(response);
}else
{
window.alert('Directions request failed due to ' + status);
}
});
}
}
}
</script>
<body onload="initialize()">
<div id="map_canvas" style="height: 600px"></div>
<p>
<b>Developed by</b>
<a href="http://www.linkedin.com/in/arnab-chakravarty-8a329111b?trk=nav_responsive_tab_profile"><b>Arnab Chakravarty</b></a>.
</p>
</body>
</html>
whereNew.js
myData = [
[22.5193768,88.3656851, 'K.M.C. Market, 3rd Floor Market Complex, 212, Rash Behari Avenue Road, Hindustan Park, Gariahat, Kolkata, West Bengal 700019, India', 'PAL BAZAR'],
[22.4977887,88.3796042, 'Garfa Main Rd, Kolkata, West Bengal 700075, India', 'RAMLAL BAZAR'],
[22.5048463,88.3882325, 'Purbachal Main Rd, Ramlal Bazar, Haltu, Kolkata, West Bengal 700078, India', 'COLLEGE STREET MARKET ( SPORTS GOODS)']
];
MarketData.js
MarketInfo = [
["GARIAHAT KMC MARKET", "K.M.C. Market, 3rd Floor Market Complex, 212, Rash Behari Avenue Road, Hindustan Park, Gariahat, Kolkata- 700019", "MAJOR (1400SHOP + HOWKER)", "FRUITS, VEGETABLES, FLOWER, FISH, MEAT, GROCERY, CLOTHING, FURNITURE , HOUSEHOLD, FOOTWEAR ELECTRICAL ETC", "", "", "BARABAZAR, KOLEYMART", "HOWRAH", "We collect the goods ourselves", "", "MANY", "Cars, Light goods vehicles (LGV), Heavy goods vehicles/lorries (HGV)", "Within one hour", "", "", "No problems encountered", "", "IF A UNLOADING SPACE CAN PROVIDE BY GOVT. IT WILL BE BETTER"],
["PAL BAZAR", "GARIFA MAIN ROAD, KOLKATA - 700 075", "MINOR (150SHOP + 70HOWKERS)", "FRESH, COOL AND FROZEN PRODUCT, OTHER TYPE OF CONSUMER GOODS, TEXTILE,FOOTWEAR, ELECTRONIC AND ELECTRICAL EQUIPMENT, BUILDING MATERIALS", "", "SANDHYA BAZAR", "BARA BAZAR, CHADNI CHOWK", "", "The goods are delivered by multiple suppliers", "MANY", "10 TO 20", "Bicycles/tricycles/carts, Motorised two/three-wheelers", "Within one hour", "", "", "Delivery vehicle causes traffic jams in front of the establishment", "", ""],
["RAMLAL BAZAR", "Purbachal Main Rd, Ramlal Bazar, Haltu. Kolkata-700078", "MINOR (150 SHOP+ 180 HOWKERS)", "FRUITS, VEGETABLE, FISH, MEAT, GROCERY, TEXTILE, ELECTRICAL, BUILDING MATERIALS ETC", "", "BAGHAJATIN AND SANDHYA BAZAR", "BARA BAZAR, EZRA ST.", "", "We collect the goods ourselves", "", "MANY", "Bicycles/tricycles/carts, Motorised two/three-wheelers, Light goods vehicles (LGV)", "Within one hour", "", "", "Limited storage space available, Delivery vehicle causes traffic jams in front of the establishment, High delivery costs", "", "PARKING PLACE REQUIRE"]
];
最佳答案
问题出在这行代码
var content = row1[2] + '<br/><button onclick = calculateAndDisplayRoute(directionsDisplay, directionsService, markerArray, map, row1[1]);>Show Routes</button>';
onclick
需要用双引号引起来的函数名称
由于您将 HTML 创建为字符串,因此您传递的参数不会被捕获。您可以尝试使用全局变量并查看结果。
编辑:
我对代码进行了以下更改
在初始化函数之外声明全局变量:
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var markerArray = [];
function initialize() {
将错误行更改为:
var content = row1[2] + '<br/><button onclick="calculateAndDisplayRoute(' + i + ')">Show Routes</button>';
注意,我正在发送行标识符 (i)。我已经删除了所有其他参数。
calculateAndDisplayRoute
的签名是:
function calculateAndDisplayRoute(i) {
var destination1 = MarketInfo[i][1];
...
}
关于javascript - 未捕获的类型错误 : Cannot read property 'route' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369876/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!