2012-03-30 210 views
1

我在asp内容页面中使用Google Maps API v3。我在地图上有一个标记,我需要将标记坐标保存在数据库中。在.aspx文件夹中,我可以在<div id="indo"><div>中显示标记坐标。如何在我的.aspx.cs文件中获得标记坐标?
Google example
我的代码:
Google Maps API v3标记坐标

<%@ Page Title="Dragable Marker Address" Language="C#" MasterPageFile="~/TesztMasterPage.Master" AutoEventWireup="true" CodeBehind="TesztContentPage2.aspx.cs" Inherits="Google1.TesztContentPage2" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
    var geocoder = new google.maps.Geocoder(); 

    function geocodePosition(pos) { 
     geocoder.geocode({ 
      latLng: pos 
     }, function (responses) { 
      if (responses && responses.length > 0) { 
       updateMarkerAddress(responses[0].formatted_address); 
      } else { 
       updateMarkerAddress('Cannot determine address at this location.'); 
      } 
     }); 
    } 

    function updateMarkerStatus(str) { 
     document.getElementById('markerStatus').innerHTML = str; 
    } 

    function updateMarkerPosition(latLng) { 
     document.getElementById('info').innerHTML = [ 
    latLng.lat(), 
    latLng.lng() 
    ].join(', '); 
    } 

    function updateMarkerAddress(str) { 
     document.getElementById('address').innerHTML = str; 
    } 

    function initialize() { 
     var latLng = new google.maps.LatLng(-34.397, 150.644); 
     var map = new google.maps.Map(document.getElementById('mapCanvas'), { 
      zoom: 8, 
      center: latLng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 
     var marker = new google.maps.Marker({ 
      position: latLng, 
      title: 'Point A', 
      map: map, 
      draggable: true 
     }); 

     // Update current position info. 
     updateMarkerPosition(latLng); 
     geocodePosition(latLng); 

     // Add dragging event listeners. 
     google.maps.event.addListener(marker, 'dragstart', function() { 
      updateMarkerAddress('Dragging...'); 
     }); 

     google.maps.event.addListener(marker, 'drag', function() { 
      updateMarkerStatus('Dragging...'); 
      updateMarkerPosition(marker.getPosition()); 
     }); 

     google.maps.event.addListener(marker, 'dragend', function() { 
      updateMarkerStatus('Drag ended'); 
      geocodePosition(marker.getPosition()); 
     }); 
    } 

    // Onload handler to fire off the app. 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
<style type="text/css"> 
    #mapCanvas { 
    width: 500px; 
    height: 400px; 
    float: left; 
    } 
    #infoPanel { 
    float: left; 
    margin-left: 10px; 
    } 
    #infoPanel div { 
    margin-bottom: 5px; 
    } 

</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <div id="mapCanvas"></div> 
    <div id="infoPanel"> 
     <b>Marker status:</b> 
     <div id="markerStatus"><i>Click and drag the marker.</i></div> 
     <b>Current position:</b> 
     <div id="info"></div> 
     <b>Closest matching address:</b> 
     <div id="address"></div> 
    </div> 
</asp:Content> 

回答

1

您可以添加一个隐藏输入

<input type="hidden" name="hiddeninputname" value="" runat="server"/> 

然后在JavaScript代码保存在它的坐标:

document.getElementById('hiddeninputname').value = yourcoordinates; 

,然后你可以用后面访问从代码隐藏的输入:

hiddeninputname.Value; 

注意:如果您有多个坐标,你可以将它们保存在由“_”或分隔字符串一个你选择的交易者。然后在后面的代码,你可以从一个字符串中提取它们

编辑 如果你想文本直接设置在JavaScript中,你可以放置一个标签在你的DIV

<label id="LabelId"></label> 

,并设置其文本到任何你想要的

document.getElementById('labelId').InnerHTML = 'Coordinate Text Goes Here'; 
+0

我试过了,但不幸的是它杀死了googlemaps api的javascript函数。任何想法通过div元素离开文本? – user1269225 2012-03-30 12:34:27

+0

我编辑了我的答案 – Youssef 2012-03-30 12:44:10

+0

document.getElementById('label')。value = ...对我来说vorked – user1269225 2012-03-30 14:28:03