在谷歌地图中放置标记

时间:2012-11-08 03:57:14

标签: javascript jquery

我的作业要求根据我们需要解析的xml文档在地图上放置多个标记。我已经完成了解析,但是我很难搞清楚如何将lat和long元素放入新的标记对象中。我的代码:

function loadPoint(){
// var newHTML= "<div style=\"padding:5px;border:solid 1px red\">";
// new_lat =  escapeText($(this).find("latitude").number();
// new_long =  escapeText($(this).find("longitude"));
// newHTML += new_lat + "," + new_long;
// newHTML += "</div>";
// $("body").append(newHTML);

var latlong= new google.maps.LatLng($(this).find("latitude"),$(this).find("longitude"));
var marker = new google.maps.Marker({
        map: map, 
        position: latlong
    })
}

function parser(data) {
$(data).find("item title").each(loadDescription);
$(data).find("item latitude").each(loadLat);
$(data).find("item longitude").each(loadLong);
$(data).find("item").each(loadPoint);
}


function myBadLoadFunction(myXMLHttpRequest,myErrorMessage,myErrorThrown) {
alert('status: ' + myErrorMessage + '\n' + myXMLHttpRequest.responseText);
}

var map;
var centerPoint = new google.maps.LatLng(53.608803,-1.497327);

function initializeMap(){
var myOptions = {
    zoom: 8,
    center: centerPoint,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

 map = new google.maps.Map($('#map_canvas').get(0), myOptions);

在我的脑海中,这是有道理的......将解析后的数据作为创建新latlng的参数,但它没有做任何事情......

2 个答案:

答案 0 :(得分:0)

首先尝试安排您的声明。

        var map; //declare it here

        function initialize(){
        var centerPoint = new google.maps.LatLng(lat, lng);
        var mapOptions = {
                  zoom: 16,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  center: centerPoint
                };
        map = new google.maps.Map(document.getElementById('map_canvas'),
                    mapOptions);
        }
        function loadPoint(){
        var marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat, lng),
                map: map
            });

        }

答案 1 :(得分:0)

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var locations = [
      [-33.890542, 151.274856],
      [-33.923036, 151.259052],
      [-34.028249, 151.157507],
      [-33.80010128657071, 151.28747820854187],
      [-33.950198, 151.259302]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][0], locations[i][1]),
        map: map
      });

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