2011-05-09 93 views
1

即時通訊使用Asp.net和谷歌地圖API。在網站地圖上是可見的和一些字段。當用戶輸入指定的地址和半徑時,我可以得到在該位置購物的列表(使用web服務)。我在服務器端做。現在我想在Google地圖上顯示結果。asp.net +谷歌地圖API,添加標記到網站

什麼是最好的方法來做到這一點?任何scropt裝載機或類似的東西?

回答

0

你可以輸出標記成一個陣列的HTML列表,然後像這樣的東西添加到地圖(未經測試,但不遠處):

var gMarkers = [['place1', 55.8, -4.020508],['place2', 53.3, -7.61],['place3', 57.9, -8.12]]; 

for (var i = 0; i < gMarkers.length; i++) { 
    var marker = new google.maps.Marker({ 
        position: new google.maps.LatLng(lat, lon), 
        map: map 
       }); 

}

你可以也使用jQuery向web服務發出請求。 如果你的web服務正在返回XML,那麼有jQuery插件可以幫助你解析客戶端的數據,但Json是更好的選擇。

您是否使用ajax將注入html添加到頁面中,或者在用戶輸入地址後是否使用回發來重新加載頁面?

0

試試這個

<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
    <title> 
 
     Google Map Demo 
 
    </title> 
 
</head> 
 
<body> 
 
    <div id="MyDivScrolling"> 
 
     <div> 
 
      <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyC8KY7rkZP7Xyq5oVM0nssUc4OPIP7MQtw&callback=initMap"></script> 
 
      <script type="text/javascript"> 
 
       var markers = [{ 
 
       "title": 'Havana', 
 
       "lat": '23.1330200', 
 
       "lng": '-82.3830400', 
 
       "description": 'José Martí International Airport, Havana' 
 
      } 
 
\t \t , 
 
\t \t \t { 
 
\t \t \t  "title": 'Tampa', 
 
\t \t \t  "lat": '27.964157', 
 
\t \t \t  "lng": '-82.452606', 
 
\t \t \t  "description": 'Tampa International Airport, Tampa' 
 
\t \t \t } 
 

 
\t \t , 
 
\t \t { 
 
\t \t  "title": 'Havana', 
 
\t \t  "lat": '23.1330200', 
 
\t \t  "lng": '-82.3830400', 
 
\t \t  "description": 'José Martí International Airport, Havana' 
 
\t \t } 
 
\t \t , 
 
\t \t  { 
 
\t \t   "title": 'Orlando', 
 
\t \t   "lat": '28.538336', 
 
\t \t   "lng": ' -81.379234', 
 
\t \t   "description": 'Orlando International Airport, Orlando' 
 
\t \t  } 
 
\t  , 
 
\t \t  { 
 
\t \t   "title": 'Havana', 
 
\t \t   "lat": '23.1330200', 
 
\t \t   "lng": '-82.3830400', 
 
\t \t   "description": 'José Martí International Airport, Havana' 
 
\t \t  } 
 
\t \t , 
 
      { 
 
       "title": 'Miami', 
 
       "lat": '25.7742700', 
 
       "lng": '-80.1936600', 
 
       "description": 'Miami International Airport, Miami' 
 
      } 
 
     , 
 
      { 
 
       "title": 'Havana', 
 
       "lat": '23.1330200', 
 
       "lng": '-82.3830400', 
 
       "description": 'José Martí International Airport, Havana' 
 
      } 
 
      , 
 
\t \t  { 
 
\t \t   "title": 'Miami', 
 
\t \t   "lat": '25.7742700', 
 
\t \t   "lng": '-80.1936600', 
 
\t \t   "description": 'Miami International Airport, Miami' 
 
\t \t  } 
 
\t \t , 
 
      { 
 
       "title": 'Camagüey-Ignacio Agramonte', 
 
       "lat": '21.416666666667', 
 
       "lng": '-77.866666666667 ', 
 
       "description": 'Camagüey-Ignacio Agramonte Airport, Camagüey' 
 
      } 
 

 
      , 
 
\t \t \t { 
 
\t \t \t  "title": 'Tampa', 
 
\t \t \t  "lat": '27.964157', 
 
\t \t \t  "lng": '-82.452606', 
 
\t \t \t  "description": 'Tampa International Airport' 
 
\t \t \t } 
 
      , 
 
       { 
 
        "title": 'Camagüey-Ignacio Agramonte', 
 
        "lat": '21.416666666667', 
 
        "lng": '-77.866666666667 ', 
 
        "description": 'Camagüey-Ignacio Agramonte Airport, Camagüey' 
 
       } 
 
       , 
 
\t \t \t { 
 
\t \t \t  "title": 'Tampa', 
 
\t \t \t  "lat": '27.964157', 
 
\t \t \t  "lng": '-82.452606', 
 
\t \t \t  "description": 'Tampa International Airport, Tampa' 
 
\t \t \t }]; 
 
       window.onload = function() { 
 
        var mapOptions = { 
 
         center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
 
         zoom: 9, 
 
         mapTypeId: google.maps.MapTypeId.HYBRID 
 
        }; 
 
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 
 
        var infoWindow = new google.maps.InfoWindow(); 
 
        var lat_lng = new Array(); 
 
        var latlngbounds = new google.maps.LatLngBounds(); 
 
        for (i = 0; i < markers.length; i++) { 
 
         var data = markers[i] 
 
         var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
 
         lat_lng.push(myLatlng); 
 
         var marker = new google.maps.Marker({ 
 
          position: myLatlng, 
 
          map: map, 
 
          title: data.title 
 
         }); 
 
         latlngbounds.extend(marker.position); 
 
         (function (marker, data) { 
 
          google.maps.event.addListener(marker, "click", function (e) { 
 
           infoWindow.setContent(data.description); 
 
           infoWindow.open(map, marker); 
 
          }); 
 
         })(marker, data); 
 
        } 
 
        map.setCenter(latlngbounds.getCenter()); 
 
        map.fitBounds(latlngbounds); 
 

 
        //***********ROUTING****************// 
 

 
        //Intialize the Path Array 
 
        var path = new google.maps.MVCArray(); 
 

 
        //Intialize the Direction Service 
 
        var service = new google.maps.DirectionsService(); 
 

 
        //Set the Path Stroke Color 
 
        var poly = new google.maps.Polyline({ map: map, strokeColor: '#DC143C' }); 
 

 
        //Loop and Draw Path Route between the Points on MAP 
 
        for (var i = 0; i < lat_lng.length; i++) { 
 
         if ((i + 1) < lat_lng.length) { 
 
          var src = lat_lng[i]; 
 
          var des = lat_lng[i + 1]; 
 
          path.push(src); 
 
          poly.setPath(path); 
 
          service.route({ 
 
           origin: src, 
 
           destination: des, 
 
           travelMode: google.maps.DirectionsTravelMode.DRIVING 
 
          }, function (result, status) { 
 
           if (status == google.maps.DirectionsStatus.OK) { 
 
            for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) { 
 
             path.push(result.routes[0].overview_path[i]); 
 
            } 
 
           } 
 
          }); 
 
         } 
 
        } 
 
       } 
 
      </script> 
 
      <div class="MainSliderItem2 ImagesContainerItem"> 
 
       <div id="dvMap" style="width: 100%;height: 490px;position: relative;overflow: hidden;background-color: rgb(229, 227, 223);max-width: 80%;margin: 98px auto;"> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</body> 
 
</html>

Output screen