0

当我使用“center: new google.maps.LatLng”这个命令时,通常我们使用这种格式(22.621177,120.298311)

但我的来源是7 05.16' N, 171 21.88' E

有人知道下一步该怎么做吗?

function load(User_Id) {
  var map = new google.maps.Map(document.getElementById("map_canvas"), {
    center: new google.maps.LatLng(22.601914,120.283186),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

var infoWindow = new google.maps.InfoWindow;
var geocoder = new google.maps.Geocoder();


  downloadUrl("db_2_xml.php?id="+User_Id, function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("markers");

    for (var i = 0; i < markers.length; i++) {
      var vessel = markers[i].getAttribute("vessel");


      var geocoder = new google.maps.Geocoder(
        geocoder.geocode(markers[i].getAttribute("GPS")));

  var speed = markers[i].getAttribute("speed");


      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: geocoder,
        icon: icon.icon,
        shadow: icon.shadow
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
4

1 回答 1

0

描述

这听起来像您需要将全球位置坐标从 DMS 格式转换7 05.16' N, 171 21.8' E为十进制格式,如22.621177, 120.298311. 在您的示例中88,秒数超过该位置允许的 60 秒。

此功能将完成从 DMS 转换为十进制格式的困难部分。它不会验证值是否在可接受的范围内,也不会将额外的值带到下一个更大的单位位置。

例子

现场示例:http: //ideone.com/h2xwAc

代码

<?php

function DMS2Decimal($degrees = 0, $minutes = 0, $seconds = 0, $direction = 'n') {
     //converts DMS coordinates to decimal
     //returns false on bad inputs, decimal on success

     //direction must be n, s, e or w, case-insensitive
     $d = strtolower($direction);
     $ok = array('n', 's', 'e', 'w');

     //degrees must be integer between 0 and 180
     if(!is_numeric($degrees) || $degrees < 0 || $degrees > 180) {
          $decimal = false;
     }
     //minutes must be integer or float between 0 and 59
     elseif(!is_numeric($minutes) || $minutes < 0 || $minutes > 59) {
          $decimal = false;
     }
     //seconds must be integer or float between 0 and 59
     elseif(!is_numeric($seconds) || $seconds < 0 || $seconds > 59) {
          $decimal = false;
     }
     elseif(!in_array($d, $ok)) {
          $decimal = false;
     }
     else {
          //inputs clean, calculate
          $decimal = $degrees + ($minutes / 60) + ($seconds / 3600);

          //reverse for south or west coordinates; north is assumed
          if($d == 's' || $d == 'w') {
               $decimal *= -1;
          }
     }

     return $decimal;
}



echo "x: " . DMS2Decimal($degrees = 7, $minutes = 5, $seconds = 16, $direction = 'n');

echo "y: " . DMS2Decimal($degrees = 171, $minutes = 21, $seconds = 8, $direction = 'e');

输出

x: 7.0877777777778y: 171.35222222222

插入你的代码

请注意,这部分答案未经测试,可能出现在错误的位置。这只是为了概述我将如何解决这个问题。再次注意,没有对任何值进行验证测试。

function load(User_Id, x, y) {

// if the x coordinate is not a number then assume it is in DMS format and convert
    if (!is_numeric(x)) {
        x = DMS2Decimal(x)
    }

// if the y coordinate is not a number then assume it is in DMS format and convert
    if (!is_numeric(y)) {
        y = DMS2Decimal(y)
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), {
        center: new google.maps.LatLng(x,y),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

}
于 2013-07-13T04:02:24.163 回答