Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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/2/jquery/74.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
Javascript 组合jquery映射脚本_Javascript_Jquery_Html_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 组合jquery映射脚本

Javascript 组合jquery映射脚本,javascript,jquery,html,google-maps,google-maps-api-3,Javascript,Jquery,Html,Google Maps,Google Maps Api 3,我有两个通过jquery渲染贴图的脚本 这里有一个: <script> var geocoder; var map; var infowindow; function initialize() { geocoder = new google.maps.Geocoder(); var loca = new google.maps.LatLng(41.7475, -74.0872); map = new google.maps.Map(document

我有两个通过jquery渲染贴图的脚本

这里有一个:

<script>
var geocoder;
var map;
  var infowindow;

  function initialize() {
    geocoder = new google.maps.Geocoder();
    var loca = new google.maps.LatLng(41.7475, -74.0872);

    map = new google.maps.Map(document.getElementById('map'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: loca,
      zoom: 12
    });

  }

  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }

  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'mouseover', function() {
      infowindow.setContent(place.name);
      infowindow.open(map, this);
        });
        }

   function codeAddress() {
   var address = document.getElementById("address").value;
   geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
     map.setCenter(results[0].geometry.location);
     var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
    var request = {
      location: results[0].geometry.location,
      radius: 50000,
      name: 'ski',
      keyword: 'mountain',
      type: ['park']
    };
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
}

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

除了添加标记外,您没有对
geolocpoint
变量执行任何操作。您需要使用
map.setCenter(geolocpoint)在某个点将地图置于用户位置的中心

这是你的小提琴:


希望这有帮助。

检查此项:并尝试找出问题所在。谢谢,这非常有用:)但是,我试图纠正的错误是如何将地图置于用户位置的中心。现在只创建了标记,但地图没有居中。哪个标记?当你寻找一个地方的时候?不,不是那样。它工作得很好。我已经在我的问题中添加了一个编辑,请看一下。好的,等我回家后,我会检查一下。谢谢你的帮助。如果我回答了你的问题,别忘了接受答案。是的,我会的。:)
<script type="text/javascript">
// Define your locations: HTML content for the info window, latitude, longitude

var locations = [

  ['<h4><a href="#">My Home</a></h4>', 19.114282,72.871037]
,

  ['<h4><a href="#">My parents home</a></h4>', 18.9443219,72.8227124]


];


// Setup the different icons and shadows
var iconURLPrefix = 'http://maps.google.com/mapfiles/ms/icons/';

var icons = [
  iconURLPrefix + 'red-dot.png',
  iconURLPrefix + 'green-dot.png',
  iconURLPrefix + 'blue-dot.png',
  iconURLPrefix + 'orange-dot.png',
  iconURLPrefix + 'purple-dot.png',
  iconURLPrefix + 'pink-dot.png',      
  iconURLPrefix + 'yellow-dot.png'
]
var icons_length = icons.length;


var shadow = {
  anchor: new google.maps.Point(15,33),
  url: iconURLPrefix + 'msmarker.shadow.png'
};

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 2,

  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  streetViewControl: false,
  panControl: false,
  zoomControlOptions: {
     position: google.maps.ControlPosition.LEFT_BOTTOM
  }
});

var infowindow = new google.maps.InfoWindow({
  maxWidth: 160
});

var marker;
var markers = new Array();

var iconCounter = 0;

// Add the markers and infowindows to the map
for (var i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon : icons[iconCounter],
    shadow: shadow
  });

  markers.push(marker);

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));

  iconCounter++;
  // We only have a limited number of possible icon colors, so we may have to restart the counter
  if(iconCounter >= icons_length){
    iconCounter = 0;
  }
}

function AutoCenter() {
  //  Create a new viewpoint bound
  var bounds = new google.maps.LatLngBounds();
  //  Go through each...
  $.each(markers, function (index, marker) {
    bounds.extend(marker.position);
  });
  //  Fit these bounds to the map
  map.fitBounds(bounds);
}
AutoCenter();
 </script> 
  // Check if user support geo-location
   if (navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(function(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var geolocpoint = new google.maps.LatLng(latitude, longitude);

    var mapOptions = {
        zoom: 8,
        center: geolocpoint,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }
    // Place a marker
    var geolocation = new google.maps.Marker({
        position: geolocpoint,
        map: map,
        title: 'Your geolocation',
        icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png'
    });
});
  }