0

我是新手,我正在互联网上阅读openstreetmap的可能性,还阅读了有关openlayers的信息...首先,我需要的是获取位置并创建相应的地图...我找到了一个很好的例子openlayers,这是代码:

<html>
  <head>
    <title>HTML5 geolocation with Openstreetmap and OpenLayers</title>
    <style type="text/css">
      html, body, #basicMap {
          width: 240;
          height: 320;
          margin: 10;
      }
    </style>

    <script src="http://openlayers.org/api/OpenLayers.js"></script>
    <script>
      function init() {
        map = new OpenLayers.Map("basicMap");
        var mapnik = new OpenLayers.Layer.OSM();
        map.addLayer(mapnik);

        navigator.geolocation.getCurrentPosition(function(position) {
            document.getElementById('anzeige').innerHTML="Latitude: " + position.coords.latitude + "   Longitude: " +
            position.coords.longitude + "<p>";
            var lonLat = new OpenLayers.LonLat(position.coords.longitude,
                                    position.coords.latitude)
                      .transform(
                                  new OpenLayers.Projection("EPSG:4326"), //transform from WGS 1984
                                              map.getProjectionObject() //to Spherical Mercator Projection
                                            );

            markers.addMarker(new OpenLayers.Marker(lonLat));

            map.setCenter(lonLat, 14 // Zoom level
            );

        });
        //map = new OpenLayers.Map("basicMap");
        //var mapnik = new OpenLayers.Layer.OSM();
        //map.addLayer(mapnik);
        map.setCenter(new
        OpenLayers.LonLat(3,3) // Center of the map
          .transform(
            new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
            new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
          ), 15 // Zoom level
         );
        var markers = new OpenLayers.Layer.Markers( "Markers" );
        map.addLayer(markers);

      }
    </script>

  </head>

  <body onload="init();">
<center>
HTML5 geolocation:
<br>
    <div id="basicMap"></div>
<br>HTML5 geolocation<br>
<br>with Openstreetmap and OpenLayers<br>
For Android Froyo,iPhone,iPAD,iPod
<br>
Your position estimated by browser geolocation API:<p>

<div id="anzeige">(will be displayed here)<p></div>
<a href="http://www.dr-bischoff.de">Andreas Bischoff</a>

<br>(view source to see how it works 
</center>
  </body>
</html>

你可以在这里看到一个活生生的例子:http : //www.pediaphon.org/~bischoff/location_based_services/ 下一步,我需要画一条线来显示遵循的规则。这是一个绘制线的实时示例(按 shift + 单击鼠标): http: //openlayers.org/dev/examples/draw-feature.html

但我是新手,我迷失了如何调用 openlayers 的 api 以便从起点到目的地画线......欢迎任何帮助

此致。

编辑: 我刚刚在标记之前复制了这段代码(在 OpenLayers 中使用 JavaScript 绘制一条带有一条线的路径),但我没有看到绘制的线:

    var lineLayer = new OpenLayers.Layer.Vector("Line Layer"); 

    map.addLayer(lineLayer);                    
    map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));                                     
    var points = new Array(
               /*I put these coords of my city*/
       new OpenLayers.Geometry.Point(-3.7904085, 37.76225609999999 ),
       new OpenLayers.Geometry.Point(-4.7904085, 39.76225609999999 )
    );

    var line = new OpenLayers.Geometry.LineString(points);

    var style = { 
      strokeColor: '#0000ff', 
      strokeOpacity: 0.5,
      strokeWidth: 5
    };

    var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
    lineLayer.addFeatures([lineFeature]);
4

1 回答 1

1

丹尼尔,

这个问题已经在这里解决了

使用 OpenLayers 在两点之间画线

于 2012-07-06T15:15:46.557 回答