2011-05-03 2 views
2

내지도 중 하나에 Google지도를 추가하려고합니다. 내에서Google지도에 서버 측 위도/경도 값 추가 javascript

코드 숨김, 내가지도에 대한 중심점, 내가지도하고 싶습니다 위도/경도의 배열이 있습니다

protected void Page_Load(object sender, EventArgs e) 
{ 
    Point center = new Point { latitude = 28.42693F, longitude = -81.4673F }; 

    List<Point> points = new List<Point>(); 
    points.Add(new Point { latitude = 28.43039F, longitude = -81.47186F }); 
    points.Add(new Point { latitude = 28.36906F, longitude = -81.56063F }); 

    Point[] pointArray = points.ToArray(); 
} 


public class Point 
{ 
    public float latitude; 
    public float longitude; 
} 

을 내 페이지를,이 자바 스크립트가 :

<script type="text/javascript"> 

    function initialize() { 
     if (GBrowserIsCompatible()) { 
      var map = new GMap2(document.getElementById("map_canvas")); 
      map.setCenter(new GLatLng(28.42693, -81.4673), 13); 
      //map.setUIToDefault(); 

      var blueIcon = new GIcon(G_DEFAULT_ICON); 
      blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png"; 
      markerOptions = { icon: blueIcon }; 


      var point = new GLatLng(28.43039, -81.47186); 
      map.addOverlay(new GMarker(point, markerOptions)); 

      point = new GLatLng(28.36906, -81.56063); 
      map.addOverlay(new GMarker(point, markerOptions)); 
     } 
    } 

</script> 

값은 테스트를 위해 현재 자바 스크립트에 하드 코딩되어 있습니다,하지만 난 코드 숨김에서 동적 값을 얻을 필요가있다. 어떻게해야합니까?

답변

0

위/긴 값의 JavaScript 배열을 만드는 문자열을 코드 숨김에 만들면 쉽고 빠르게 접근 할 수 있습니다. 그런 다음 .ASPX에 문자열을 추가하고 문자열 값으로 설정할 수 있습니다. 또는 귀하의 포인트에 대한 JSON 표현을 작성하십시오. 작은 일회성 시나리오에서도 효과적입니다. 그래서 자바 스크립트는이처럼 보이는 바람 수 있습니다

<script type="text/javascript"> 

    function initialize() { 
     if (GBrowserIsCompatible()) { 
      var map = new GMap2(document.getElementById("map_canvas")); 
      <asp:Literal id="litMapCenter" runat="server"/> 
      //map.setUIToDefault(); 

      var blueIcon = new GIcon(G_DEFAULT_ICON); 
      blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png"; 
      markerOptions = { icon: blueIcon }; 

      <asp:Literal id="litMapPoints" runat="server"/> 
     } 
    } 

</script> 

귀하의에서 코드 숨김 그런 다음 적절한 자바 스크립트 litMapPoints 및 litMapCenter을 설정합니다.

0

당신은

ClientScript.RegisterStartupScript(this.getType(), "whateveryourkeyis", string.Format("longitude={0};", pointArray[0].longitude), true); 

뒤에 코드에서 단순히 '경도'라는 JScript의 변수를 생성하고 .NET 코드 값으로 초기화됩니다이 방법을 이런 식으로 뭔가를 사용할 수 있습니다.

(이것은 버그가있는 경우 용서해 주므로 작성되었습니다 :-))