5

我正在建立一个使用地理位置代码的网站,它假设加载 mapCanvas 并在用户点击“找到我!”后 按钮获取他的位置并根据用户位置设置地图中心。它在 Firefox、Chrome、Safari 上运行良好,在普通 PC 和 iPhone 上进行了测试,唯一不兼容的设备是任何搭载 Android 的手机。这是一个代码:

<script src="http://maps.google.com/maps?file=api&v=3&key=YourKey" 
       type="text/javascript"></script>
<script type="text/javascript">
    //<![CDATA[

    var iconBlue = new GIcon(); 
    iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(17, 25);
    iconBlue.shadowSize = new GSize(1, 1);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconRed.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconRed.iconSize = new GSize(17, 25);
    iconRed.shadowSize = new GSize(1, 1);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["restaurant"] = iconBlue;
    customIcons["bar"] = iconRed;

    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(50.061795,19.936924), 16);

        // Change this depending on the name of your PHP file
        GDownloadUrl("Your_xml.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, type);
            map.addOverlay(marker);
          }
        });
      }
    }

    function createMarker(point, name, address, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + name + "</b> <br/>" + address;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
    //]]>


function findLoc(){
if (!navigator.geolocation) {
alert('Sorry, your browser does not support Geo Services');
}
else {
// Get the current location    
navigator.geolocation.getCurrentPosition(showMap);
}
}
function showMap(position){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var myPoint = new GLatLng(lat, lon);
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(myPoint, 15);
GDownloadUrl("Your_xml.php", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, name, address, type);
            map.addOverlay(marker);

}
       });

function createMarker(point, name, address, type) {
      var marker = new GMarker(point, customIcons[type]);
      var html = "<b>" + name + "</b> <br/>" + address;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
}

我找不到任何解决方案让它工作。PS 没有任何错误或陈述给出任何提示,使其更易于理解。设备启用了 GPS。请帮助我已经尝试了 2 周

4

1 回答 1

3

恐怕你面前只有一个艰巨的老式调试。我的方法是在代码的各个重要点(例如,在调用之前和之后getCurrentPosition,以及在开头showMap)添加可见输出,并从那里缩小发生故障的位置。一旦你知道什么是失败的,你可能会想出一个为什么,但直到你知道你在黑暗中挣扎。

(我开发了一个支持 Android 的基于地理位置的移动网络应用程序,所以如果你需要任何保证,你的情况本质上没有什么不可能的。)

于 2012-05-16T16:33:06.587 回答