3

I want to load some points, whose lat-long values are saved in a .txt file over Google maps. The following is the code which I am using for the same. The Google map layer is successfully loaded if I keep lines 1-10 & 13-17. For loading the data points I am including 11-12 lines but I am not getting any output at all. I request you to please find the error and suggest how to do the same. "data.txt" contains the lat-long values with tab delimited present in my server.

1.<html>
2.<head> 
3.<title>OpenLayers Example</title> 
4.<script src="http://openlayers.org/api/OpenLayers.js"></script> 
5.</head> 
6.<body> 
7.<div style="width:100%; height:100%" id="map"></div> 
8.<script defer="defer" type="text/javascript"> 
9.   var map = new OpenLayers.Map('map'); 
10.  var gmaplayer = new OpenLayers.Layer.Google( "Google Streets", 
       "http://maps.google.com/mapsfile=api&v=2&key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ"); 
11.  var layer = new OpenLayers.Layer.GML("Layer Name","http://localhost/data.txt", 
        {format: OpenLayers.Format.Text }); 
12.   map.addLayer(layer); 
13.   map.addLayer(gmaplayer); 
14.   map.zoomToMaxExtent(); 
15.</script> 
16.</body> 
17.</html> 

When I am replacing above 11-12 lines with

11.var pois = new OpenLayers.Layer.Text( "My Points", 
                { location:"./data.txt", 
                  projection: map.displayProjection 
                }); 
12.map.addLayer(pois);

Even then I am not able to get the output. I require some needful help from any one of you

2 Answers 2

3

I thinks there's perhaps a projection confusion between your points and the google map. In your second line n°11, you are telling OL that your points are projected in the map.displayProjection. Or you said earlier that they were in lat/long, so not projected.

Try to tell OL that the points are in the lat/long system with :

var pois = new OpenLayers.Layer.Text( "My Points", {location:"./data.txt", projection: new OpenLayers.Projection("EPSG:4326")} );
2

Are you following the specifications desribed here: http://dev.openlayers.org/docs/files/OpenLayers/Layer/Text-js.html for your text file?

The first row of the data file should be a header line with the column names of the data. Each column should be delimited by a tab space.
The possible columns are:

  • point lat,lon of the point where a marker is to be placed
  • lat Latitude of the point where a marker is to be placed
  • lon Longitude of the point where a marker is to be placed
  • icon or image URL of marker icon to use.
  • iconSize Size of Icon to use
  • iconOffset Where the top-left corner of the icon is to be placed relative to the latitude and longitude of the point.
  • title The text of the ‘title’ is placed inside an ‘h2’ marker inside a popup, which opens when the marker is clicked.
  • description The text of the ‘description’ is placed below the h2 in the popup. this can be plain text or HTML.

Check How to overlay lat/lon points on a Google layer in OpenLayers 2? for a solution to your problem of incorrectly positioned and moving points. Basically, you have to add

sphericalMercator: true,

to your Google layer definition, plus some transformations.

4
  • hello Underdark, Now I am able to mark my points on the map. But the problem is the markers are not in exact position, the markers are moving away when I am zooming in. I guess this is because of my input data.txt but it contains only 2 columns lat and lon both seperated by tabs in each row. Can you please tell me what to do? Commented Jan 30, 2011 at 19:23
  • Could you share a few lines of your .txt?
    – underdark
    Commented Jan 30, 2011 at 19:42
  • lat lon 17.22 78.29 12.17 76.41 25.20 83.00 12.58 77.38 25.35 85.12 16.31 80.39 20.15 85.50 13.08 80.19 9.55 78.1 21.08 79.1 28.38 77.17 All the values are tab delimited.I donno whether they are measured in WSG84 or Indian Geodetic Datum. Does this affect? Commented Jan 31, 2011 at 4:53
  • @user1795: I updated my answer to contain a solution for "the markers are moving away when I am zooming in".
    – underdark
    Commented Apr 23, 2011 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.