首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否未定义OMS?

是否未定义OMS?
EN

Stack Overflow用户
提问于 2017-11-30 22:42:30
回答 1查看 220关注 0票数 0

在开发人员工具中检查我的代码时,我收到以下错误

代码语言:javascript
复制
Uncaught ReferenceError: oms is not defined
at map.1.js:94
at create_marker (map.1.js:96)

我的应用程序没有使用“oms.addmarker(标记)”正确映射点;但是,它使用常规的“markers.push(标记);”,所以我认为可能是实例化导致了这个错误,如果有人能告诉我需要更改什么,将不胜感激。

代码语言:javascript
复制
var markers = []; // To erase markers later
var user_lat = 52.358409; // Random default location
var user_lng = -1.549072;
/*global geocoder*/
/*global google*/
/*global map*/
/*global draggable_marker*/
/*global custom_icons*/
/*global OverlappingMarkerSpiderfier*/
/*global new_lat*/
/*global navigator*/
/*global create_crime_markers*/
/*global new_lng*/
/*global marker*/

function map_callback(){
  // Without var = set to global scope
  geocoder = new google.maps.Geocoder();
  var new_location = new google.maps.LatLng(user_lat, user_lng);
  var map_properties = {center: new_location, zoom: 15, mapTypeId: "hybrid", zoomControlOptions: {style: google.maps.ZoomControlStyle.SMALL, position: google.maps.ControlPosition.LEFT_BOTTOM}, streetViewControlOptions:{position: google.maps.ControlPosition.LEFT_BOTTOM}};
  map = new google.maps.Map(document.getElementById("google_map"), map_properties);
  var iw = new google.maps.InfoWindow();
  var oms = new OverlappingMarkerSpiderfier(map, { 
    
        markersWontMove: true,   // we promise not to move any markers, allowing optimizations
        markersWontHide: true,   // we promise not to change visibility of any markers, allowing optimizations
        basicFormatEvents: true  // allow the library to skip calculating advanced formatting information
      });
  draggable_marker = new google.maps.Marker({
      position: new_location,
      map: map,
      draggable: true,
      title: "Drag me",
      icon: "./img/blue_marker.png"
  });
  google.maps.event.addListener(draggable_marker, "dragend", function(){draggable_callback();});
  google.maps.event.addListener(map, "click", function(event){draggable_callback(event.latLng);});
  draggable_callback(); // Trigger first load
  
       for (var i = 0, len = marker.length; i < len; i ++) {
        (function() {  // make a closure over the marker and marker data
          var markerData = marker[i];  // e.g. { lat: 50.123, lng: 0.123, text: 'XYZ' }
          var marker = new google.maps.Marker({ position: markerData });  // markerData works here as a LatLngLiteral
          google.maps.event.addListener(marker, 'spider_click', function(e) {  // 'spider_click', not plain 'click'
            iw.setContent(marker.title);
            iw.open(map, marker);
          });
          oms.addMarker(marker);  // adds the marker to the spiderfier _and_ the map
          console.log(marker);
        })();
      }
}

function search(){
  var address = document.getElementById("search_box").value;
  if (address != ""){
    geocoder.geocode( {
        "address": address,
        componentRestrictions: {country: "UK"}
      },
      function(results, status){
        if (status == "OK") {
          var loc = results[0].geometry.location
          draggable_callback(loc);
          map.panTo(loc);
        } else {
          alert("Cannot perform search, reason: " + status);
        }
    });
  }
}

function clear_markers(){
  for (var i = 0; i < markers.length; i++){
    markers[i].setMap(null);
  }
  markers = [];
}

function create_marker(lat, lng, title){
  var current_lat_lng = lat.toString() + lng.toString();
        (function() {  // make a closure over the marker and marker data
    // Default icon
    var custom_icon = "https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi.png";
    if (title in custom_icons) {custom_icon = custom_icons[title];}
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        map: map,
        icon: custom_icon,
        title: title});
      google.maps.event.addListener(marker, 'spider_click', function(e) {  // 'spider_click', not plain 'click'
          });
            oms.addMarker(marker);

        })();
      }
  

function draggable_callback(loc){
  if (loc != undefined) {draggable_marker.setPosition(loc);}

  new_lat = draggable_marker.getPosition().lat();
  new_lng = draggable_marker.getPosition().lng();

  console.log(new_lat, new_lng);
  clear_markers();
  create_crime_markers(new_lat, new_lng);
}

function get_my_loc(){
  if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(success_callback, error_callback);
  }
}

function success_callback(position){
  var new_location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  draggable_callback(new_location);
  map.panTo(new_location);
  
}

function error_callback(error){
  switch(error.code){
    case error.PERMISSION_DENIED:
      alert("Denied request for Geolocation");
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Your location information is unavailable");
      break;
    case error.TIMEOUT:
      alert("The request to get your location timed out");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error in finding your location occurred");
      break;
  }
}
代码语言:javascript
复制
<div id="google_map"></div>

代码语言:javascript
复制
    var markers = []; // To erase markers later
var user_lat = 52.358409; // Random default location
var user_lng = -1.549072;
/*global geocoder*/
/*global google*/
/*global map*/
/*global draggable_marker*/
/*global custom_icons*/
/*global OverlappingMarkerSpiderfier*/
/*global new_lat*/
/*global navigator*/
/*global create_crime_markers*/
/*global new_lng*/
/*global marker*/

function map_callback(){
  // Without var = set to global scope
  geocoder = new google.maps.Geocoder();
  var new_location = new google.maps.LatLng(user_lat, user_lng);
  var map_properties = {center: new_location, zoom: 15, mapTypeId: "hybrid", zoomControlOptions: {style: google.maps.ZoomControlStyle.SMALL, position: google.maps.ControlPosition.LEFT_BOTTOM}, streetViewControlOptions:{position: google.maps.ControlPosition.LEFT_BOTTOM}};
  map = new google.maps.Map(document.getElementById("google_map"), map_properties);
  var iw = new google.maps.InfoWindow();
  var oms = new OverlappingMarkerSpiderfier(map, { 

        markersWontMove: true,   // we promise not to move any markers, allowing optimizations
        markersWontHide: true,   // we promise not to change visibility of any markers, allowing optimizations
        basicFormatEvents: true  // allow the library to skip calculating advanced formatting information
      });
  draggable_marker = new google.maps.Marker({
      position: new_location,
      map: map,
      draggable: true,
      title: "Drag me",
      icon: "./img/blue_marker.png"
  });
  google.maps.event.addListener(draggable_marker, "dragend", function(){draggable_callback();});
  google.maps.event.addListener(map, "click", function(event){draggable_callback(event.latLng);});
  draggable_callback(); // Trigger first load

       for (var i = 0, len = marker.length; i < len; i ++) {
        (function() {  // make a closure over the marker and marker data
          var markerData = marker[i];  // e.g. { lat: 50.123, lng: 0.123, text: 'XYZ' }
          var marker = new google.maps.Marker({ position: markerData });  // markerData works here as a LatLngLiteral
          google.maps.event.addListener(marker, 'spider_click', function(e) {  // 'spider_click', not plain 'click'
            iw.setContent(marker.title);
            iw.open(map, marker);
          });
          oms.addMarker(marker);  // adds the marker to the spiderfier _and_ the map
          console.log(marker);
        })();
      }
}

function search(){
  var address = document.getElementById("search_box").value;
  if (address != ""){
    geocoder.geocode( {
        "address": address,
        componentRestrictions: {country: "UK"}
      },
      function(results, status){
        if (status == "OK") {
          var loc = results[0].geometry.location
          draggable_callback(loc);
          map.panTo(loc);
        } else {
          alert("Cannot perform search, reason: " + status);
        }
    });
  }
}

function clear_markers(){
  for (var i = 0; i < markers.length; i++){
    markers[i].setMap(null);
  }
  markers = [];
}

function create_marker(lat, lng, title){
  var current_lat_lng = lat.toString() + lng.toString();
        (function() {  // make a closure over the marker and marker data
    // Default icon
    var custom_icon = "https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi.png";
    if (title in custom_icons) {custom_icon = custom_icons[title];}
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        map: map,
        icon: custom_icon,
        title: title});
      google.maps.event.addListener(marker, 'spider_click', function(e) {  // 'spider_click', not plain 'click'
          });
            oms.addMarker(marker);

        })();
      }


function draggable_callback(loc){
  if (loc != undefined) {draggable_marker.setPosition(loc);}

  new_lat = draggable_marker.getPosition().lat();
  new_lng = draggable_marker.getPosition().lng();

  console.log(new_lat, new_lng);
  clear_markers();
  create_crime_markers(new_lat, new_lng);
}

function get_my_loc(){
  if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(success_callback, error_callback);
  }
}

function success_callback(position){
  var new_location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  draggable_callback(new_location);
  map.panTo(new_location);

}

function error_callback(error){
  switch(error.code){
    case error.PERMISSION_DENIED:
      alert("Denied request for Geolocation");
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Your location information is unavailable");
      break;
    case error.TIMEOUT:
      alert("The request to get your location timed out");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error in finding your location occurred");
      break;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-01-06 08:28:53

也许我漏掉了什么,但我没有在你的html头中看到oms:

代码语言:javascript
复制
 <script src="https://cdnjs.cloudflare.com/ajax/libs/OverlappingMarkerSpiderfier/1.0.3/oms.min.js">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47576069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档