A.计算两点距离

public const double EarthRadiusKm = 6378.137; // WGS-84 
public double GetDistance(double p1Lat, double p1Lng,
                          double p2Lat, double p2Lng)
{

    double dLat1InRad = p1Lat * (Math.PI / 180);
    double dLong1InRad = p1Lng * (Math.PI / 180);
    double dLat2InRad = p2Lat * (Math.PI / 180);
    double dLong2InRad = p2Lng * (Math.PI / 180);
    double dLongitude = dLong2InRad - dLong1InRad;
    double dLatitude = dLat2InRad - dLat1InRad;
    double a = Math.Pow(Math.Sin(dLatitude / 2), 2)
                      + Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) 
                      * Math.Pow(Math.Sin(dLongitude / 2), 2);
    double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    double dDistance = EarthRadiusKm * c;
    return dDistance;
}  

 

 

B.获取经纬度,

 ApiUrl: http://maps.google.co.uk/maps/geo?q= + searchkey + &output=xml

            http://maps.google.fr/maps/geo?q= + searchkey + &output=xml

 searchkey=housenum.Trim()+" "+street.Trim()+" "+city.Trim()+" "+postcode.Trim()).Replace(" ", "+")

public bool GetCoordinate(string ApiUrl, string searchkey,
                      out double logitute, out double Latitude)
{

    string xmldoc = "";
    logitute=0;
    Latitude=0;
    HttpWebRequest request;
    HttpWebResponse response = null;
    Stream stream;
    string resultStr = "0,0";
    try
    {
        request = (HttpWebRequest)WebRequest.Create(ApiUrl);
        response = (HttpWebResponse)request.GetResponse();
        stream = response.GetResponseStream();
        xmldoc = new StreamReader(stream,
                    System.Text.Encoding.Default).ReadToEnd();
        stream.Close();
        response.Close();
    }
    catch (Exception ex)
    {

    }
    //xml demo
    //<Point>
    //<coordinates>116.4074130,39.9042140,0</coordinates>
    //</Point>

    int startPosition = xmldoc.IndexOf("<coordinates>");
    int endPosition = xmldoc.IndexOf("</coordinates>");
    if (startPosition >= 0 && endPosition >= 0)
    {
        string coordi = xmldoc.Substring(startPosition,
                                endPosition - startPosition);
        int douposition = coordi.LastIndexOf(",");
        coordi = coordi.Substring(0, douposition);
        resultStr = coordi.Replace("<coordinates>", "")
                          .Replace("</coordinates>", "");
    }
    else
    {
        resultStr = "no";
    }

    if (!resultStr.Equals("no"))
    {
        var p=resultStr.Split(',');
        if (p.Length==2)
        {
            var style = NumberStyles.AllowDecimalPoint;
            var culture =CultureInfo.CreateSpecificCulture("en-GB");
            return (double.TryParse(p[0], style, culture, out logitute) 
                && double.TryParse(p[1], style, culture, out Latitude));
        }
    }
    return false;

}
 

 

 C.Google Map API V3,设置某地经纬度.

 

    var gMap;
    var gPoint;
    var gMarker;
    var gMapRegion="NL";
    var gMarkerImg= 'bg_A.png';
  function initialize() {
      
            gPoint = new google.maps.LatLng(52.3702157, 4.8951679);
        var myOptions = {
            zoom: 8,
            center:gPoint,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        gMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        gMarker = new google.maps.Marker({
        position: gPoint,
        draggable:true,
        map: gMap,
        icon: gMarkerImg
        });

  

    google.maps.event.addListener(gMap, 'rightclick', function (event) {        
        gMarker.setPosition(event.latLng)
       
    });

   
    google.maps.event.addListener(gMarker, 'position_changed', function () {
        var p = gMarker.getPosition();   
        $("#txtlat").val(p.lat().toFixed(7));
        $("#txtlng").val(p.lng().toFixed(7));
    });
    google.maps.event.addListener(gMarker, 'mouseover', function () {
        SetTipPosition(gMarker, 150, 150);
    });

    
    google.maps.event.addListener(gMarker, 'mouseout', function () {
          $("#flowerInfo").css("display","none");
      });

      function SetTipPosition(marker,tipwidth,tipheight) {
          var ne = gMap.getBounds().getNorthEast();
          var sw = gMap.getBounds().getSouthWest();
          var height = Math.abs(ne.lat() - sw.lat());
          var width = Math.abs(ne.lng() - sw.lng());

          var mp = marker.getPosition();
          var x = Math.abs(mp.lng() - sw.lng());
          var y = Math.abs(mp.lat() - ne.lat());

          var distanceMark = 10;
          var xx = 1.00 * x / width * $("#map_canvas").width() + distanceMark;
          var yy = 1.00 * y / height * $("#map_canvas").height()+ distanceMark;
          if (xx + tipwidth > $("#map_canvas").width())
              xx = xx - tipwidth - 2 * distanceMark;
          if (yy + tipheight > $("#map_canvas").height())
              yy = yy - tipheight - 2 * distanceMark;

          var content = "lat:" + marker.getPosition().lat().toFixed(7) 
+ ",lng:" + marker.getPosition().lng().toFixed(7)
+ ",Zoom:" + gMap.getZoom(); ShowTip(xx + "px", yy + "px",content); } } function ShowTip(x, y, content) { if ($("#flowerInfo").length == 0) { $("#map_canvas div:first div:first").append("<div id='flowerInfo'
style='z-index: 230; position: absolute;
display:none;width:150px;height:150px;background-color:write'><b>"
+ content + "</b></div>"); } else { $("#flowerInfo").html("<b>"+content+"</b>") } $("#flowerInfo").css({ left: x, top: y, display: "block" }); } $(function () { initialize(); if ($("#txtlat").val() != '' && $("#txtlng").val() != '') { var p = new google.maps.LatLng($("#txtlat").val(), $("#txtlng").val()) gMarker.setPosition(p); gMap.setCenter(p); } $("#btnSearch").click(function () { var address = $("#txtKeyword").val(); var geocoder = new google.maps.Geocoder(); if (geocoder) { geocoder.geocode({ "address": address, "region": gMapRegion }, function (results, status) { if (status == google.maps.GeocoderStatus.OK && results.length > 0 &&
results[0].address_components[results[0].address_components.length - 1].short_name == gMapRegion) { gMarker.setPosition(results[0].geometry.location); gMarker.setIcon(gMarkerImg); var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < results.length; i++) { if (results[i].address_components.pop().short_name == gMapRegion) bounds.union(results[i].geometry.bounds); } //for gMap.fitBounds(bounds); gMap.setZoom((gMap.getZoom() > 10) ? gMap.getZoom() - 2 : 8); } //if else { gMarker.setIcon(); gMarker.setPosition(gPoint); gMap.setCenter(gPoint); gMap.setZoom(8); } }); } // if }); // click }); //jquery
 <input id="txtKeyword" type="text" value="Hoorn"/>
 <input id="btnSearch" type="button" value="Search"/>
 <input id="txtlat" type="text" value="52.7625419"/>
 <input id="txtlng" type="text" value="6.8946796"/>
 <div id="map_canvas" style="width:100%; height:80%;" >
 </div> 

 

 

 

 

posted on 2012-10-22 14:49  极简  阅读(761)  评论(0编辑  收藏  举报