0

我有以下代码不起作用。当 Jquery 和 javascript 访问同一个变量时,我不知道如何将它们集成到同一个文件中......就像这里 latitude1 和 longitude1 是两个变量,两者都必须使用。请帮助使其工作

<!DOCTYPE html>
<html>
  <head>
    <title>Place searches</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script>


    var latitude1, longitude1;


$(document).ready(function () {

  $(":button").click(function(){

     var add = $("#destination").val();

    $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address= + add +&sensor=false", function (data) {


           var latitude1 =  data.results[0].geometry.location.lat; 
           vat longitude1 = data.results[0].geometry.location.lng;



    });
    });
});

var map;
var infowindow;

function initialize() {
  var pyrmont = new google.maps.LatLng(latitude1, longitude1);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: pyrmont,
    zoom: 15
  });

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

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, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

  </script>
  </head>
  <body> 
  <input type="text"  id="destination" /><button type="button">Search</button><br>
    <div id="map-canvas" style="width: 50%; float:left"></div>
    <div style="width:46%; float:left">
   </body>
</html>
4

1 回答 1

1

如果你写 var 你再次定义变量所以删除 var

$(文档).ready(函数 () {

$(":button").click(function(){

 var add = $("#destination").val();

$.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address= + add +&sensor=false", function (data) {


      latitude1 =  data.results[0].geometry.location.lat; 
      longitude1 = data.results[0].geometry.location.lng;



});
});

});

于 2013-08-04T08:16:58.673 回答