Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 15, 2006, 05:12   #1
seannie
SitePoint Enthusiast
 
Join Date: Nov 2006
Posts: 79
Javascript Help

Atteched is a bit of script I´m using to try and calculate the distance between two points on google maps. Could someone possibly check the code in bold at the bottom and tell me whether I need to make any changes and what these are.

Thanks for your help.

******************************************

<script type="text/javascript">
//<![CDATA[
var map,geocoder,FloristIcon,HotIcon,ColdIcon,PointIcon;


function showAddress(address) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
map.addOverlay(createMarker(point, address,PointIcon));
}
}
);
}

function showAddress(address1) {
geocoder.getLatLng(
address1,
function(point) {
if (!point) {
alert(address1 + " not found");
} else {
map.setCenter(point, 13);
map.addOverlay(createMarker(point, address1,PointIcon));
}
}
);
}


function createMarker(point, desc, Icon) {
var marker = new GMarker(point, Icon);

GEvent.addListener(marker, "click", function() {
map.setCenter(marker.getPoint(), 16);
map.setMapType(G_SATELLITE_TYPE);
marker.openInfoWindowHtml(desc);
});

return marker;
}

function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
geocoder = new GClientGeocoder();

// Create icons
PointIcon = new GIcon();
PointIcon.image = "img/mm_point.png";
PointIcon.shadow = "img/mm_fmem_sh.png";
PointIcon.iconSize = new GSize(30, 35);
PointIcon.shadowSize = new GSize(40, 35);
PointIcon.iconAnchor = new GPoint(6, 20);
PointIcon.infoWindowAnchor = new GPoint(5, 1);

FloristIcon = new GIcon();
FloristIcon.image = "img/mm_fmem.png";
FloristIcon.shadow = "img/mm_fmem_sh.png";
FloristIcon.iconSize = new GSize(30, 35);
FloristIcon.shadowSize = new GSize(40, 35);
FloristIcon.iconAnchor = new GPoint(6, 20);
FloristIcon.infoWindowAnchor = new GPoint(5, 1);

HotIcon = new GIcon();
HotIcon.image = "img/mm_fhot.png";
HotIcon.shadow = "img/mm_fhot_sh.png";
HotIcon.iconSize = new GSize(12, 20);
HotIcon.shadowSize = new GSize(22, 20);
HotIcon.iconAnchor = new GPoint(6, 20);
HotIcon.infoWindowAnchor = new GPoint(5, 1);

ColdIcon = new GIcon();
ColdIcon.image = "img/mm_fcold.png";
ColdIcon.shadow = "img/mm_fcold_sh.png";
ColdIcon.iconSize = new GSize(12, 20);
ColdIcon.shadowSize = new GSize(22, 20);
ColdIcon.iconAnchor = new GPoint(6, 20);
ColdIcon.infoWindowAnchor = new GPoint(5, 1);




// Add 10 markers to the map at random locations
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i = 0; i < 10; i++) {
var point = new GLatLng(southWest.lat() + latSpan * Math.random(),southWest.lng() + lngSpan * Math.random());
map.addOverlay(createMarker(point, "Florist member: <b>"+ i + 1 +"</b>",FloristIcon));
}
map.addOverlay(createMarker(new GLatLng(37.4419, -122.1419), "Hot lead:",HotIcon));
map.addOverlay(createMarker(new GLatLng(37.4489, -122.1419), "Cold lead:",ColdIcon));
}
}

var info_html="Straight line distances<br>";
for (var address = 0; address<markers.length; address++) {
for (var address1=0; address1<markers.length; address1++) {
if (address != address1) {
var distance=address.getPoint().distanceFrom(address1.getPoint())/1000;


var html = "<b>From "+address.label+ " To "+address1.label;
html += "</b> "+ distance.toFixed(5) +" kilometres ";
html += " Bearing: "+bearing(address.getPoint(),address1.getPoint())+" degrees<br>";


info_html += html;
}
}
}

//]]>
</script>
seannie is offline   Reply With Quote
Old Nov 15, 2006, 18:47   #2
adrained
SitePoint Enthusiast
 
Join Date: Oct 2006
Posts: 38
A few things:
1) markers is not defined
Code:
markers = new Array()
function createMarker(point, desc, Icon) 
{
	var marker = new GMarker(point, Icon);
	GEvent.addListener(marker, "click", function() {
		map.setCenter(marker.getPoint(), 16);
		map.setMapType(G_SATELLITE_TYPE);
		marker.openInfoWindowHtml(desc);
		});
	markers[markers.length] = marker;
	return marker;
}
2) the code at the bottom needs to be in the load function or called from the load function. Currently it is executed before the map is loaded.

3) bearing? there was no function for this. I don't think google provides one.

4) where you reference address and address1 inside the loop you need to use
markers[address] and markers[address1]
Code:
for (var address = 0; address<markers.length; address++) 
{
	for (var address1=0; address1<markers.length; address1++) 
	{
markers[address] 

markers[address1]
	}
}
5) .label also seems incorrect.


That's all I could figure out for now... but it is a start.
adrained is offline   Reply With Quote
Old Nov 16, 2006, 02:07   #3
seannie
SitePoint Enthusiast
 
Join Date: Nov 2006
Posts: 79
thank a lot for your help.

I really appreciate it.
seannie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 13:20.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved