[OpenLayers-Users] Google and Markers and Vectors ... oh my!

Peterson Eric-EEP002 Eric.Peterson at motorola.com
Wed Nov 28 10:23:12 EST 2007


Okay, here's my test case ... Any idea what I'm missing?

-eP


<script>

// Our default list of colors
var colors = [ 'red', 'yellow', 'green', 'cyan', 'blue', 'magenta' ];

// The RGB codes for each color name
var colorCode = {
	red:     '#ff0000',
	yellow:  '#ffff00',
	green:   '#00ff00',
	cyan:    '#00ffff',
	blue:    '#0000ff',
	magenta: '#ff00ff'
};

var markerPath = "images/marker-";
var markerSuffix = ".png";

// A container for the style of each color
var style = {};

// Build the style for each color
for (var col in colors)
{
	style[colors[col]] = {
		strokeColor:   colorCode[colors[col]],
		strokeOpacity: 0.8,
		strokeWidth:   3,
		fillColor:     colorCode[colors[col]],
		pointRadius:   6,
		pointerEvents: "visiblePainted"
	};
}

// Default trail data
var data = [
	{
		tag: 'Set 1',
		points: [
			new OpenLayers.LonLat (-80.100, 26.100),
			new OpenLayers.LonLat (-80.110, 26.105),
			new OpenLayers.LonLat (-80.120, 26.110),
			new OpenLayers.LonLat (-80.130, 26.115),
			new OpenLayers.LonLat (-80.140, 26.120)
		]
	},
];

function olinit ()
{
	var myMap = new OpenLayers.Map ('map', {
		controls: [
			new OpenLayers.Control.MouseDefaults(),
			new OpenLayers.Control.LayerSwitcher(),
			new OpenLayers.Control.PanZoomBar(),
			new OpenLayers.Control.MousePosition(),
			new OpenLayers.Control.Scale(),
			new OpenLayers.Control.DragPan()
		],
		tileSize: new OpenLayers.Size (256, 256),
projection: "EPSG:900913",
	});

	// Google Maps
	myMap.addLayer (new OpenLayers.Layer.Google("Google Normal",
{ type: G_NORMAL_MAP   }, { 'sphericalMercator': true }));
	myMap.addLayer (new OpenLayers.Layer.Google("Google Satellite",
{ type: G_SATELLITE_MAP}, { 'sphericalMercator': true }));
	myMap.addLayer (new OpenLayers.Layer.Google("Google Hybrid",
{ type: G_HYBRID_MAP   }, { 'sphericalMercator': true }));

	// Markers
	var myMarkerLayer = new OpenLayers.Layer.Markers ("Markers");
	myMap.addLayer (myMarkerLayer);

	// Add a new vector layer
	var myVectorLayer = new OpenLayers.Layer.Vector ("Vector
Features");
	myMap.addLayer (myVectorLayer);

	// Walk through each feature to draw
	for (var f = 0 ; f < data.length ; ++f)
	{
		// Build a new pointlist from the data
		var pointList = [];

		// Add the data points to the list
		for (var p = 0 ; p < data[f].points.length ; ++p)
		{
			// Add a marker
			myMarkerLayer.addMarker (new OpenLayers.Marker (
				data[f].points[p],
				new OpenLayers.Icon
("images/marker-red.png", new OpenLayers.Size(20, 20))
			));

			// Create a new point
			var myPoint = new OpenLayers.Geometry.Point
(data[f].points[p].lon, data[f].points[p].lat);

			// Push the point onto the list
			pointList.push (myPoint);

			// Create a new point feature for this point
			var myPointFeature = new
OpenLayers.Feature.Vector (myPoint, null, style[colors[f]]);
			myVectorLayer.addFeatures ([myPointFeature]);
		}

		// Now build a new line feature from the points
		var lineFeature = new OpenLayers.Feature.Vector (
			new OpenLayers.Geometry.LineString (pointList),
null, style[colors[f]]
		);

		// Add this line to the map
		myVectorLayer.addFeatures ([lineFeature]);
	}

	// Point to home
	myMap.setCenter (new OpenLayers.LonLat (-80, 26), 10);
}

</script> 

-----Original Message-----
From: Christopher Schmidt [mailto:crschmidt at metacarta.com] 
Sent: Wednesday, November 28, 2007 10:02 AM
To: Peterson Eric-EEP002
Cc: users at openlayers.org
Subject: Re: [OpenLayers-Users] Google and Markers and Vectors ... oh
my!

On Wed, Nov 28, 2007 at 09:53:15AM -0500, Peterson Eric-EEP002 wrote:
  
> It seems to me that it's behaving as though the markers are being 
> projected correctly over the base, which is spherical Mercator, but 
> the line feature is being projected according to EPSG:4326. But that's

> just a guess ... I'm not familiar enough with the guts of how that 
> thing works.
>  
> Is there something special I need to do to get Google, the markers, 
> and vector layer features to all sync up?

It sounds an awful lot like you're not really using Spherical Mercator.
Can you share a small piece of sample code that reproduces this
behavior?

Regards,
--
Christopher Schmidt
MetaCarta



More information about the Users mailing list