TypeError:undefined不是对象(评估'type.toString')(TypeError: undefined is not an object (evaluati

编程入门 行业动态 更新时间:2024-10-26 21:33:03
TypeError:undefined不是对象(评估'type.toString')(TypeError: undefined is not an object (evaluating 'type.toString'))

在过去的三个小时里,我一直在努力解决这个问题。 我不知道我还能尝试什么。 我希望在单击某个按钮“id”时设置var undecidedType, undecidedFilter 。 我不知道我做错了什么。 也许有人可以帮忙。

这是我的.js:

function getLocation() { Modernizr.geolocation ? navigator.geolocation.getCurrentPosition(currentLocation, handle_error, { timeout: 1e4 }) : ($wait.fadeOut(), $locationBar.fadeIn()) } function handle_error(e) { $locationBar.css("opacity", 1), showError("Location not found"), 0 == e.code, 1 == e.code, 2 == e.code, 3 == e.code } function currentLocation(e) { $wait.fadeIn(), $locationBar.fadeOut(); var o = e.coords.latitude, t = e.coords.longitude; var undecidedType, undecidedFilter; var b1 = document.getElementById('rest'), b2 = document.getElementById('bar'); b1.onclick = function() { undecidedType = "restaurant"; undecidedFilter = "restaurant"; }; b2.onclick = function() { undecidedType = "bars"; undecidedFilter = "pubs"; }; currentlatlng = new google.maps.LatLng(o, t), getPlaces(currentlatlng, undecidedType) } function getPlaces(e, type) { userLoc = e, homeMarker = new google.maps.Marker({ map: map, animation: google.maps.Animation.DROP, position: e, icon: homeIcon }); var o = { location: e, radius: 1e3, rankby: "prominance", keyword: type.toString() }; service.search(o, storeRequestBar) } function storeRequestBar(e, undecidedFilter) { barResultsStore = e; var o = { location: currentlatlng, radius: 1e3, rankby: "prominance", keyword: undecidedFilter.toString() }; service.search(o, storeRequestPub) } function storeRequestPub(e) { for (pubResultsStore = e, totalResults = barResultsStore.concat(pubResultsStore), resultsStore = removeDupes(totalResults, "id"), resultsStore = resultsStore.sort(function() { return Math.random() - .5 }), i = 0; i < resultsStore.length; i++); 0 == resultsStore && showError("Cannot find anything in that location. Try somewhere else"), chooseBar(resultsStore) } function chooseBar(e) { barRef = { reference: e[shitCounter].reference }, service.getDetails(barRef, showBar) } function showBar(e, o, hello) { if (o == google.maps.places.PlacesServiceStatus.OK) { for (i in markersArray) markersArray[i].setMap(null); drinkMarker = new google.maps.Marker({ map: map, animation: google.maps.Animation.DROP, position: e.geometry.location, icon: drinkIcon }), markersArray.push(drinkMarker), placeName = e.name, calcRoute(userLoc, e.geometry.location), directionsDisplay.setMap(null), directionsDisplay.suppressMarkers=!0, directionsDisplay.polylineOptions = { strokeColor: "#ff8400", strokeOpacity: .8, strokeWeight: 5 }, directionsDisplay.setMap(map), placeSite = e.website ? e.website : e.url, placeAddress = e.formatted_address, $(".recommendation__destination").html("Why don't you go to <br/><a href='" + placeSite + "' target='_blank'>" + placeName + "</a>" + currentLocation.hello + ""), $(".map__address").html(placeAddress), $(".grid__row--links,.grid__row--actions,.fyyfs,.recommendation,.map__address,.recommendation__destination,.adsense").fadeIn(function() { $wait.fadeOut() }) } } function calcRoute(e, o) { var t = { origin: e, destination: o, travelMode: google.maps.TravelMode.WALKING }; directionsService.route(t, function(e, o) { o == google.maps.DirectionsStatus.OK && directionsDisplay.setDirections(e) }) } function codeAddress() { $wait.fadeIn(function() { $locationBar.fadeOut(); var e = document.getElementById("locationsearch").value; geocoder.geocode({ address: e }, function(e, o) { o == google.maps.GeocoderStatus.OK ? (currentlatlng = e[0].geometry.location, getPlaces(currentlatlng)) : showError("Location not found. Try again") }) }) } function showError(e) { $wait.fadeOut(), $locationBar.fadeIn(), $(".locator__message").text(e).fadeIn() } function removeDupes(e, o) { var t = [], r = {}; for (var a in e) r[e[a][o]] = e[a]; for (a in r) t.push(r[a]); return t } var resultref, marker, markersArray = [], shitCounter = 0, iteration = 0, drinkIcon = "img/restaurant_map_pointer_small-2.png", homeIcon = "img/current_location_map_pointer_small-2.png", resultsStore, totalResults = [], pubResultsStore, barResultsStore, userLoc, currentlatlng, undecidedType, undecidedFilter; $wait = $(".loader"), $locationBar = $(".locator"), $mapCanvas = $(".map__canvas"), $type = $(".type"); var lowSat = [{ featureType: "all", stylers: [{ saturation: 1, strokeColor: "red", strokeOpacity: .8, strokeWeight: 5 } ] } ], myOptions = { zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP, styles: lowSat, mapTypeControl: !1, panControl: !1, zoomControl: !0, mapTypeControl: !1, scaleControl: !1, streetViewControl: !1, overviewMapControl: !1 }; map = new google.maps.Map(document.getElementById("googlemap"), myOptions), geocoder = new google.maps.Geocoder; var service = new google.maps.places.PlacesService(map); $(document).ready(function() { var e = window.location.href, o = e.split("?"); "undcd" == o[1] ? $locationBar.css("opacity", 1) : getLocation() }); var directionsDisplay = new google.maps.DirectionsRenderer({ suppressMarkers: !0 }), directionsService = new google.maps.DirectionsService; $(".locator").on("submit", function(e) { codeAddress(), e.preventDefault() }), $(".actions__shit").click(function() { return shitCounter < resultsStore.length - 1 ? shitCounter++ : shitCounter = 0, chooseBar(resultsStore), !1 }); var autoOptions = { types: ["geocode"] }, autoInput = document.getElementById("locationsearch"); autocomplete = new google.maps.places.Autocomplete(autoInput, autoOptions);

这是html表单:

<form class="form" class="locator" action="#"> <button id="rest" class="btn" type="submit" value="restaurant">two</button><br> <button id="bar" class="btn" type="submit" value="bar">one</button><br> </form>

这是错误:

TypeError: undefined is not an object (evaluating 'type.toString') getPlacesapp-min.js:37 currentLocationapp-min.js:24

I have been struggling with this for the past three hours. I have no idea what else I can try. I want the var undecidedType, undecidedFilter to be set when a certain button "id" is clicked. I have no idea what I am doing wrong. Maybe someone can help.

Here is my .js:

function getLocation() { Modernizr.geolocation ? navigator.geolocation.getCurrentPosition(currentLocation, handle_error, { timeout: 1e4 }) : ($wait.fadeOut(), $locationBar.fadeIn()) } function handle_error(e) { $locationBar.css("opacity", 1), showError("Location not found"), 0 == e.code, 1 == e.code, 2 == e.code, 3 == e.code } function currentLocation(e) { $wait.fadeIn(), $locationBar.fadeOut(); var o = e.coords.latitude, t = e.coords.longitude; var undecidedType, undecidedFilter; var b1 = document.getElementById('rest'), b2 = document.getElementById('bar'); b1.onclick = function() { undecidedType = "restaurant"; undecidedFilter = "restaurant"; }; b2.onclick = function() { undecidedType = "bars"; undecidedFilter = "pubs"; }; currentlatlng = new google.maps.LatLng(o, t), getPlaces(currentlatlng, undecidedType) } function getPlaces(e, type) { userLoc = e, homeMarker = new google.maps.Marker({ map: map, animation: google.maps.Animation.DROP, position: e, icon: homeIcon }); var o = { location: e, radius: 1e3, rankby: "prominance", keyword: type.toString() }; service.search(o, storeRequestBar) } function storeRequestBar(e, undecidedFilter) { barResultsStore = e; var o = { location: currentlatlng, radius: 1e3, rankby: "prominance", keyword: undecidedFilter.toString() }; service.search(o, storeRequestPub) } function storeRequestPub(e) { for (pubResultsStore = e, totalResults = barResultsStore.concat(pubResultsStore), resultsStore = removeDupes(totalResults, "id"), resultsStore = resultsStore.sort(function() { return Math.random() - .5 }), i = 0; i < resultsStore.length; i++); 0 == resultsStore && showError("Cannot find anything in that location. Try somewhere else"), chooseBar(resultsStore) } function chooseBar(e) { barRef = { reference: e[shitCounter].reference }, service.getDetails(barRef, showBar) } function showBar(e, o, hello) { if (o == google.maps.places.PlacesServiceStatus.OK) { for (i in markersArray) markersArray[i].setMap(null); drinkMarker = new google.maps.Marker({ map: map, animation: google.maps.Animation.DROP, position: e.geometry.location, icon: drinkIcon }), markersArray.push(drinkMarker), placeName = e.name, calcRoute(userLoc, e.geometry.location), directionsDisplay.setMap(null), directionsDisplay.suppressMarkers=!0, directionsDisplay.polylineOptions = { strokeColor: "#ff8400", strokeOpacity: .8, strokeWeight: 5 }, directionsDisplay.setMap(map), placeSite = e.website ? e.website : e.url, placeAddress = e.formatted_address, $(".recommendation__destination").html("Why don't you go to <br/><a href='" + placeSite + "' target='_blank'>" + placeName + "</a>" + currentLocation.hello + ""), $(".map__address").html(placeAddress), $(".grid__row--links,.grid__row--actions,.fyyfs,.recommendation,.map__address,.recommendation__destination,.adsense").fadeIn(function() { $wait.fadeOut() }) } } function calcRoute(e, o) { var t = { origin: e, destination: o, travelMode: google.maps.TravelMode.WALKING }; directionsService.route(t, function(e, o) { o == google.maps.DirectionsStatus.OK && directionsDisplay.setDirections(e) }) } function codeAddress() { $wait.fadeIn(function() { $locationBar.fadeOut(); var e = document.getElementById("locationsearch").value; geocoder.geocode({ address: e }, function(e, o) { o == google.maps.GeocoderStatus.OK ? (currentlatlng = e[0].geometry.location, getPlaces(currentlatlng)) : showError("Location not found. Try again") }) }) } function showError(e) { $wait.fadeOut(), $locationBar.fadeIn(), $(".locator__message").text(e).fadeIn() } function removeDupes(e, o) { var t = [], r = {}; for (var a in e) r[e[a][o]] = e[a]; for (a in r) t.push(r[a]); return t } var resultref, marker, markersArray = [], shitCounter = 0, iteration = 0, drinkIcon = "img/restaurant_map_pointer_small-2.png", homeIcon = "img/current_location_map_pointer_small-2.png", resultsStore, totalResults = [], pubResultsStore, barResultsStore, userLoc, currentlatlng, undecidedType, undecidedFilter; $wait = $(".loader"), $locationBar = $(".locator"), $mapCanvas = $(".map__canvas"), $type = $(".type"); var lowSat = [{ featureType: "all", stylers: [{ saturation: 1, strokeColor: "red", strokeOpacity: .8, strokeWeight: 5 } ] } ], myOptions = { zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP, styles: lowSat, mapTypeControl: !1, panControl: !1, zoomControl: !0, mapTypeControl: !1, scaleControl: !1, streetViewControl: !1, overviewMapControl: !1 }; map = new google.maps.Map(document.getElementById("googlemap"), myOptions), geocoder = new google.maps.Geocoder; var service = new google.maps.places.PlacesService(map); $(document).ready(function() { var e = window.location.href, o = e.split("?"); "undcd" == o[1] ? $locationBar.css("opacity", 1) : getLocation() }); var directionsDisplay = new google.maps.DirectionsRenderer({ suppressMarkers: !0 }), directionsService = new google.maps.DirectionsService; $(".locator").on("submit", function(e) { codeAddress(), e.preventDefault() }), $(".actions__shit").click(function() { return shitCounter < resultsStore.length - 1 ? shitCounter++ : shitCounter = 0, chooseBar(resultsStore), !1 }); var autoOptions = { types: ["geocode"] }, autoInput = document.getElementById("locationsearch"); autocomplete = new google.maps.places.Autocomplete(autoInput, autoOptions);

Here is the html form:

<form class="form" class="locator" action="#"> <button id="rest" class="btn" type="submit" value="restaurant">two</button><br> <button id="bar" class="btn" type="submit" value="bar">one</button><br> </form>

Here is the error:

TypeError: undefined is not an object (evaluating 'type.toString') getPlacesapp-min.js:37 currentLocationapp-min.js:24

最满意答案

当您第一次调用getPlaces , type是未定义的,因为当用户单击特定按钮时会定义getPlaces 。

在getPaces中更改此行:

keyword: type.toString()

keyword: type ? type.toString() : ""

或者将默认值设置为undecidedType

要修复单击侦听器,请将其更改为:

b1.onclick = function() { undecidedType = "restaurant"; undecidedFilter = "restaurant"; currentlatlng = new google.maps.LatLng(o, t); getPlaces(currentlatlng, undecidedType); }; b2.onclick = function() { undecidedType = "bars"; undecidedFilter = "pubs"; currentlatlng = new google.maps.LatLng(o, t); getPlaces(currentlatlng, undecidedType) };

type is undefined when you first call getPlaces, since undecidedType is defined when the user clicks in a specific button.

Change this line in getPaces:

keyword: type.toString()

to

keyword: type ? type.toString() : ""

Or set a default value to undecidedType

To fix your click listeners, change the to this:

b1.onclick = function() { undecidedType = "restaurant"; undecidedFilter = "restaurant"; currentlatlng = new google.maps.LatLng(o, t); getPlaces(currentlatlng, undecidedType); }; b2.onclick = function() { undecidedType = "bars"; undecidedFilter = "pubs"; currentlatlng = new google.maps.LatLng(o, t); getPlaces(currentlatlng, undecidedType) };

更多推荐

本文发布于:2023-04-29 23:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   undefined   TypeError   type   evaluating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!