Javascript 多位置谷歌地图

Javascript 多位置谷歌地图,javascript,google-maps,Javascript,Google Maps,如何在谷歌地图上显示多个位置。 我在谷歌地图上使用这个代码 <script type="text/javascript"> $(function() { // when the document is ready to be manipulated. if (GBrowserIsCompatible()) { // if the browser is compatible with Google Map's

如何在谷歌地图上显示多个位置。 我在谷歌地图上使用这个代码

<script type="text/javascript">
          $(function() { // when the document is ready to be manipulated.

              if (GBrowserIsCompatible()) { // if the browser is compatible with Google Map's
                  var map = document.getElementById("myMap"); // Get div element
                  var m = new GMap2(map); // new instance of the GMap2 class and pass in our div location.

                  var longArray= ("<?php echo $long; ?>").split(',');
                  var latArray= ("<?php echo $lat; ?>").split(',');

                 for(i=0;i<longArray.length;i++)
                 {
                     m.setCenter(new GLatLng(latArray[i], longArray[i]), 13); // pass in latitude, longitude, and zoom level.
                     m.openInfoWindow(m.getCenter(), document.createTextNode("This is testing")); // displays the text

                 }

                m.setMapType(G_SATELLITE_MAP); // sets the default mode. G_NORMAL_MAP, G_HYBRID_MAP

                    var c = new GMapTypeControl(); // switch map modes
                    m.addControl(c);

                    m.addControl(new GLargeMapControl()); // creates the zoom feature


              }
              else {
                  alert("Upgrade your browser, man!");
              }
          });
      </script>

$(function(){//当文档准备好进行操作时。
if(GBrowserIsCompatible()){//如果浏览器与Google地图的兼容
var map=document.getElementById(“myMap”);//Get div元素
var m=newgmap2(map);//GMap2类的新实例,并在我们的div位置传递。
变量longArray=(“”)。拆分(“,”);
var latArray=(“”)。拆分(“,”);

对于(i=0;isetCenter用于缩放和居中地图。如果要标记多个位置,需要创建一个标记并将其放置在循环内的地图上。这里有一组很好的教程:

如果您的问题涉及在一张地图上显示多人的不同位置,则您不能,一张地图一次只能以一个lat/lng为中心

还不完全清楚你想要实现什么


Dunc.

您必须遵循以下步骤。 1.用javascript aaray列出你的地址。 2.创建一个实用函数来进行地理编码,然后通过传递地址作为参数来放置标记。 3.迭代地址数组并调用标记实用程序函数

示例:map.jsp:: 它输入json字符串,该字符串是地址列表,并将其转换为javascript数组: 通过从google下载添加jquery和infobox.js

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
   <%@ page contentType="text/html;charset=windows-1252"%>
     <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>
         <script language="JavaScript" src="js/jquery-1.8.0.min.js"       type="text/javascript"></script>
      <script language="JavaScript" src="js/infobox.js" type="text/javascript"></script>

     </head> 
   <body>
   <%
       String json=request.getParameter("address");
    %>
       <input type="hidden" id="json" value="<%=json%>"></input>

       <div id="map" style="width: 1250px; height: 500px;" align="center"></div>

         <script type="text/javascript" language="JavaScript" src="js/map.js"></script>
          <script type="text/javascript">
            var jsonvalue=document.getElementById("json").value;
         var use=unescape(jsonvalue);
       //alert(use);
              var obj = eval ("(" + use + ")"); 
    var cobj=obj.center;
       var olist=obj.other;
     codeproject(cobj.center_add,cobj.center_name);
      //alert(cobj.center_name+" and "+cobj.center_add);
     for(var i=0;i<olist.length;i++)
     {
     //alert(olist[i].other_add);
     codeAddress(olist[i].other_add,olist[i].other_name);
     }

      </script>
     </body>
     </html>

谷歌地图多个标记
var jsonvalue=document.getElementById(“json”).value;
var use=unescape(jsonvalue);
//警惕(使用);
var obj=评估(“(“+使用+”);
var cobj=对象中心;
var olist=对象其他;
代码项目(cobj.center\U add,cobj.center\U name);
//警报(cobj.center\u名称+”和“+cobj.center\u添加);

对于(var i=0;i请参考下面的代码,这对我来说非常好。 下面的代码片段将为您提供一个提供有效API密钥的错误,即“Google Maps JavaScript API错误:InvalidKeyMapError”,要解决此问题,您只需要Google Maps提供的有效API密钥


使用谷歌地图的多个地点
变量位置数组=[
[Pune',18.5248904,73.7228789,1],
[“孟买”,19.0825223,72.7410977,2],
[Ahmednagar',19.1104918,74.6728675,3],
[Surat',21.1594627,77.3507354,4],
[Indore',22.7242284,75.7237617,5]
];
var map=new google.maps.map(document.getElementById('googleMap'){
缩放:8,
中心:新google.maps.LatLng(18.5248904,73.7228789),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
如果多个位置的代码出现问题,我如何使用此代码?
       //used by infowindow
        //the googlemap code
   var geocoder = new google.maps.Geocoder();
  //var infowindow = new google.maps.InfoWindow();
   var LatLngList = new Array(6);
    var  i;
        var infowindow = new google.maps.InfoWindow();

     var markerBounds = new google.maps.LatLngBounds();
       var markerarray=new Array();
         //making the div for window popup
    var boxText = document.createElement("div");
        boxText.style.cssText = "border: 2px solid Gray; margin-top: 6px;       background:           white; padding: 5px;font-weight: bold;color: Gray;";
    boxText.innerHTML = " ";

    //options array for infobox window
     var myOptions = {
       map:map,
         content : boxText,
         disableAutoPan : false,
         maxWidth : 0,
         pixelOffset : new google.maps.Size( - 140, 0),
         zIndex : null,
         boxStyle :  { background : "url('tipbox.gif') no-repeat",  width : "280px" },
         closeBoxMargin : "10px 4px 2px 2px", closeBoxURL : "close.gif", 
         infoBoxClearance : new google.maps.Size(1, 1), 
         isHidden : false,
         pane : "floatPane",
         enableEventPropagation : true
     };
      var infoBox;
     function codeproject(address, client) {    
         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, icon : 'green-dot.png', position : results                      [0].geometry.location, draggable : false, animation : google.maps.Animation.DROP
        });
        //bounce the marker
       // marker.setAnimation(google.maps.Animation.BOUNCE);
        //initialize info box
        infoBox = new InfoBox(myOptions); 
        markerBounds.extend(results[0].geometry.location);
        //listeners
        google.maps.event.addListener(marker, 'mouseover', function () {
        //stop bouncing
       //   marker.setAnimation(null);
         // $("img[src$='iws3.png']").hide();
         //   infowindow.setContent('<b>' + client + '<\/b><br>'+ results[0].formatted_address);
         //  infowindow.open(map, this);
          boxText.innerHTML = "<br>"+client +"<br>"+results[0].formatted_address;

          infoBox.setContent(boxText,marker);
          infoBox.open(map,marker);
            });
             google.maps.event.addListener(marker, 'mouseout', function () {
                 // infowindow.close();
              infoBox.close();
                //start bounce
                //    marker.setAnimation(google.maps.Animation.BOUNCE);
        });

        //ok end
    }
    else {
        if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            alert("Error Occured during geocode:" + status);
        }
        //  alert('Geocode was not successful for '+client +' the following reason: ' + status);
    }
        });
       }

         function codeAddress(address, client) {
//  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);
       infoBox = new InfoBox(myOptions); 

      var  marker = new google.maps.Marker( {
            map : map, 
          // icon : 'smallpin.png',
            position : results[0].geometry.location, draggable : false, animation : google.maps.Animation.DROP
        });
        //make bounds
     //bounce the marker
       // marker.setAnimation(google.maps.Animation.BOUNCE);
        //initialize info box
        markerBounds.extend(results[0].geometry.location);
        //listeners
        google.maps.event.addListener(marker, 'mouseover', function () {
        //stop bouncing
      //  marker.setAnimation(null);
           $("img[src$='iws3.png']").hide();
           // infowindow.setContent('<b>' + client + '<\/b><br>'+ results[0].formatted_address + '<\br>');
            //infowindow.open(map, this);
               boxText.innerHTML = "<br>"+client +"<br>"+results[0].formatted_address ;

          infoBox.setContent(boxText,marker);
          infoBox.open(map,marker);


        });
        google.maps.event.addListener(marker, 'mouseout', function () {
          //  infowindow.close();
         //start bounce
         infoBox.close();
       //   marker.setAnimation(google.maps.Animation.BOUNCE);
        });

        //ok end
    }
    else {
        //  alert('Geocode was not successful for '+client +' the following reason: ' + status);
    }
      });
         }


           //////////////calling the above two functions
      var centerpoint = new google.maps.LatLng(43.652527,  - 79.381961);//for ontario          canada zoom level-7
    //map intializing
     var map = new google.maps.Map(document.getElementById('map'), 
       {
zoom : 4, backgroundColor : '#B5B5B5', draggable : true, center : centerpoint,             mapTypeId : google.maps.MapTypeId.ROADMAP
         });
     ///geocoding multiple addresses
    //bounce markers
    function toggleBounce(mark) {
if (mark.getAnimation() != null) {

    mark.setAnimation(null); 
}
else {
    mark.setAnimation(google.maps.Animation.BOUNCE);
}
    }

    /////
    function putmarker(address,client,lat,lng) {  

    var position = new google.maps.LatLng(lat,lng);
      var  marker = new google.maps.Marker( {
            map : map, icon : 'green-dot.png', position : position, draggable : false,         animation : google.maps.Animation.DROP
        });
        //bounce the marker
       // marker.setAnimation(google.maps.Animation.BOUNCE);
        //initialize info box
        infoBox = new InfoBox(myOptions); 
        markerBounds.extend(position);
        //listeners
        google.maps.event.addListener(marker, 'mouseover', function () {
        //stop bouncing
       //   marker.setAnimation(null);
         // $("img[src$='iws3.png']").hide();
         //   infowindow.setContent('<b>' + client + '<\/b><br>'+ results[0].formatted_address);
         //  infowindow.open(map, this);
          boxText.innerHTML = "<br>"+client +"<br>"+address;

          infoBox.setContent(boxText,marker);
          infoBox.open(map,marker);
        });
        google.maps.event.addListener(marker, 'mouseout', function () {
         //  infowindow.close();
          infoBox.close();
         //start bounce
     //    marker.setAnimation(google.maps.Animation.BOUNCE);
        });

        //ok end
    }