Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps api 3 谷歌地图API v3初始化函数_Google Maps Api 3 - Fatal编程技术网

Google maps api 3 谷歌地图API v3初始化函数

Google maps api 3 谷歌地图API v3初始化函数,google-maps-api-3,Google Maps Api 3,我想整理一下我的initialize()函数。。。但每次我这么做,它都会破坏我的密码。 我最终尝试做一些事情,比如,我有一个侧边栏,里面有一些与地图上的标记直接相关的东西,AJAX在。。。首先,我希望能够将其他函数放在initialize()函数之外这很好用: <script> function initialize() { // create the map object var mapOptions = { zoom: 14, map

我想整理一下我的
initialize()
函数。。。但每次我这么做,它都会破坏我的密码。 我最终尝试做一些事情,比如,我有一个侧边栏,里面有一些与地图上的标记直接相关的东西,AJAX在。。。首先,我希望能够将其他函数放在
initialize()
函数之外这很好用:

<script>

  function initialize() {
    // create the map object
    var mapOptions = {
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };    
    var map = new google.maps.Map(document.getElementById("map_canvas"), 
        mapOptions);

    // create your location marker
    var mylocOptions = {
      draggable: true,
      animation: google.maps.Animation.DROP,
      icon: new google.maps.MarkerImage('//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png',
            new google.maps.Size(22,22),
            new google.maps.Point(0,18),
            new google.maps.Point(11,11)),
      title: "You are here..."
    };
    var myloc = new google.maps.Marker(mylocOptions);

    // get location information from browser, or from user input, or from database
    <% if !signed_in? || !current_user.loc %>
      if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function(pos) {
        var me = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
        myloc.setPosition(me);
        myloc.setMap(map);
        map.setCenter(me);
        $.ajax({
          data: { me: me.toString() },
          type: 'POST',
          url: '/set-location'
        })
      }, function(error) {
          var address = prompt('Where are you looking?');
          geocoder = new google.maps.Geocoder();
          geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              var me = results[0].geometry.location
              myloc.setPosition(me);
              myloc.setMap(map);
              map.setCenter(me);
            } else {
              alert("Geocode was not successful for the following reason: " + status);
            };
          });
      });
    <% else %>
      var me = new google.maps.LatLng(<%= current_user.loc %>);
      myloc.setPosition(me);
      myloc.setMap(map);
      map.setCenter(me);
      map.setZoom(12);
    <% end %>

    // watch for marker movement, and update location accordingly
    var oldPos = myloc.getPosition();
    google.maps.event.addListener(myloc, "dragend", function(e){
      revGeo = new google.maps.Geocoder();
      var newPos = myloc.getPosition();
      $.ajax({
        data: { me: newPos.toString() },
        type: 'GET',
        url: '/set-location'
      })
      if(oldPos != newPos)
        revGeo.geocode({'latLng': newPos}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
              $('#loc').html(results[1].formatted_address);
            }
          } else {
            alert("Geocoder failed due to: " + status);
          }
        });
      oldPos = newPos;
    });

    // when creating an event, check for event location,
    // verify it's existance and put a marker down on the map
    $(document).on('focusout', '#event_location', function() {
      geocoder = new google.maps.Geocoder();
      address = document.getElementById("event_location").value;
      geocoder.geocode({ 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var coords = results[0].geometry.location;
          map.setCenter(coords);
          var marker = new google.maps.Marker({
            map: map,
            animation: google.maps.Animation.DROP,
            position: coords
          });
          $('#coords').html('coordinates: ' + coords)
          $('#event_geocode').val(coords.toString())
        } else {
          alert(status + " for " + address);
        };
      });
    });
  }
</script>

函数初始化(){
//创建贴图对象
变量映射选项={
缩放:14,
mapTypeId:google.maps.mapTypeId.ROADMAP
};    
var map=new google.maps.map(document.getElementById(“map_canvas”),
地图选项);
//创建您的位置标记
变量mylocOptions={
真的,
动画:google.maps.animation.DROP,
图标:新的google.maps.MarkerImage(“//maps.gstatic.com/mapfiles/mobile/mobileimgs2.png”,
新谷歌地图大小(22,22),
新谷歌地图点(0,18),
新google.maps.Point(11,11)),
标题:“你在这里…”
};
var myloc=new google.maps.Marker(mylocOptions);
//从浏览器、用户输入或数据库获取位置信息
if(navigator.geolocation)navigator.geolocation.getCurrentPosition(函数(pos){
var me=new google.maps.LatLng(pos.coords.lation,pos.coords.longitude);
myloc.设置位置(me);
myloc.setMap(map);
地图设置中心(me);
$.ajax({
数据:{me:me.toString()},
键入:“POST”,
url:“/设置位置”
})
},函数(错误){
var address=prompt('你在找什么?');
geocoder=新的google.maps.geocoder();
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var me=results[0]。geometry.location
myloc.设置位置(me);
myloc.setMap(map);
地图设置中心(me);
}否则{
警报(“地理编码因以下原因未成功:“+状态”);
};
});
});
var me=new google.maps.LatLng();
myloc.设置位置(me);
myloc.setMap(map);
地图设置中心(me);
map.setZoom(12);
//观察标记移动,并相应地更新位置
var oldPos=myloc.getPosition();
google.maps.event.addListener(myloc,“dragend”,函数(e){
revGeo=new google.maps.Geocoder();
var newPos=myloc.getPosition();
$.ajax({
数据:{me:newPos.toString()},
键入:“GET”,
url:“/设置位置”
})
if(oldPos!=newPos)
地理代码({'latLng':newPos},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
$('#loc').html(结果[1]。格式化的#u地址);
}
}否则{
警报(“地理编码器因“+状态”而失败);
}
});
oldPos=newPos;
});
//创建事件时,请检查事件位置,
//验证它是否存在,并在地图上放置一个标记
$(document).on('focusout','事件位置',函数(){
geocoder=新的google.maps.geocoder();
地址=document.getElementById(“事件位置”).value;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
var coords=results[0]。geometry.location;
地图设置中心(coords);
var marker=new google.maps.marker({
地图:地图,
动画:google.maps.animation.DROP,
职位:coords
});
$('#coords').html('坐标:'+coords)
$('#事件地理代码').val(coords.toString())
}否则{
警报(状态+表示+地址);
};
});
});
}
。。。但我相信它可以被打破。你知道我做错了什么吗