Anyone good with Google Maps API v3 and PHP? I need help!
Page 1 of 1
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Wed, 28th Dec 2011 22:58    Post subject: Anyone good with Google Maps API v3 and PHP? I need help!
Please enlist here... Smile


Space for rent. Contact me for rates!
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Wed, 28th Dec 2011 23:21    Post subject:
Back to top
garus
VIP Member



Posts: 34200

PostPosted: Wed, 28th Dec 2011 23:26    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:28; edited 1 time in total
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 10:28    Post subject:
I am a relative newbie, and have been recently trying to get to grips with the Google Maps v3 API.


I'm hoping someone here will be kind enough to take pity and help me with some difficulty I am having.


First, let me set the scene. I'm helping a friend develop a website for a hobby. It's a simple, PHP / MySQL based site which lists different establishments. There is a database of companies, which includes a name, address, long lats, etc.


I have managed to write the SQL query to poll the database, and return a list of markers on a map of London:


http://datanalyst.co.uk/dev/flmap/mapexample7a.php


However, I am getting confused regarding the markers, it would seem like my code (which can be found below) is iterating through all of the companies, and then setting just one marker for all points?


The code can be found at http://paste.bradleygill.com/index.php?paste_id=348277


I am hoping you can see what I'm doing here. The PHP script is printing HTML / JavaScript code to the page, which includes a foreach loop, which grabs the long lats and puts them into google.maps.LatLng


I am just just struggling with the markers - and I cannot see where I've gone wrong!


Thanks so much in advance for any help you can provide me!


Space for rent. Contact me for rates!
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 14:28    Post subject:
To be clear, the issue is that when you click on one of the points, it shows the same information for all of the points - the information from the last company...


Space for rent. Contact me for rates!
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 29th Dec 2011 14:43    Post subject:
Running through it now, already spotted a couple of errors Smile
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 29th Dec 2011 15:09    Post subject:
Gonna make a few more changes for you HD, as you can make this so much easier on yourself.

Your error with the maps API itself is quite simple though. You have a global infowindow which you redefine 28 times - so that's why the last one is the content for all of them. The click handler for each marker uses that global window, but doesn't fix its contents. What you SHOULD be doing is rewrite the click handler like this (semi pseudo code to keep things easy to read):
google.maps.event.addListener(markers[$index], 'click', function () {
var infowindow = new google.maps.InfoWindow({
content: '<b>$value</b><br><br>Company $index Name'
});
infowindow.open(map, markers[$index]);
});

In other words, build the info window in the click handler Smile

Give me a few minutes and I'll have a much cleaner solution for you though Very Happy
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 15:33    Post subject:
Ahhhhh i SEEE

you are a LEGEND.

Thanks SO MUCH.

I knew I voted for you for a reason.

TOP DAWG!


Space for rent. Contact me for rates!
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 15:38    Post subject:
btw I can PM you the db user/pass if you want to test it


Space for rent. Contact me for rates!
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 29th Dec 2011 16:33    Post subject:
Already tested it locally with a dummy array Razz

Work intervened, gotta do some emergency fixes for some silly yanks (Razz) but I'll send you a cleaner version of the whole thing in a bit. Feel free to add me on Skype as well if you use that, not really hard to find me Wink
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 16:35    Post subject:
legend, I will do that now.

totally no rush on this dude, I just massively appreciate you taking the time to help!


Space for rent. Contact me for rates!
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 29th Dec 2011 18:49    Post subject:
http://pastebin.com/vh8nAQQm

Requires PHP 5.2+ (any host with less than that is shit anyway) for json_encode. Or you'll need to install a PECL extension, or fall back to a library.

Basically though the key is to fuck around less with echo'ing JS from PHP and other shit like that. Just rely on JS; all you're doing in PHP is echo'ing JS statements, might as well just write the statements properly. This is how I/we do it whenever I/we need some data from the backend in JS; use a JSON encoded array. Don't fuck around with getting the values directly into the script, just use an intermediate variable. Makes it far easier to manage the code on both ends Smile

Also a few tips for the future HD:
- Use foreach()'s $key => $value notation - LOTS. For numerical arrays it'll be the item's index, which is perfect for any situation where you need a counter of sorts. Only exists for the duration of the loop of course, just remember that arrays in PHP and JS start at 0 (so if you want to output a "logical" number, just echo index+1).
- Limit the number of variables you use, there's no point in keeping multiple. That's why I merged $cords and $comps, it's far easier to just store each row entirely, means you can always access that data again if you need to.
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Thu, 29th Dec 2011 23:29    Post subject:
Yo, just wanted to say a massive thanks. I've not been able to review this tonight, some unexpected family events meaning I need to spend some time offline - however I'll check it all out tomorrow!

I did manage to put it onto the site though:

http://datanalyst.co.uk/dev/flmap/mapexample7b.php

Looking really good - thanks. I'll come back with my endless questions tomorrow - which of course I won't expect you to answer but appriciate any time you have / will give!

Once again, thanks so much, you're a legend!!!


Space for rent. Contact me for rates!
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Fri, 30th Dec 2011 17:26    Post subject:
Hey dude,

So I finally took a look, really good - thanks so much. It's so much more simple now!

I've been fiddling a bit, so you can find the latest version at:

http://datanalyst.co.uk/dev/flmap/mapexample7c.php

And the code at:

http://pastebin.com/jkJSkcD8

However, there are still a few further improvements I need to make:

- When you open one marker, the previous one needs to close. I looked on the net, and found this: http://stackoverflow.com/questions/1875596/have-just-one-infowindow-open-in-google-maps-api-v3 - It sounds like in your implementation we create a new infowindow ever time a marker is clicked, instead of re-using the same one? Is this easy for me to change?

- When the map loads, I want it to look at all co-ordinates and then positive the map accordingly, with all markers central to the screen. I think it could be as simple as using something like map.fitBounds (bounds); but I've not had enough time to research this. Any advice / help greatly appriciated here...

- Animations. I love the fact that your code moves the map to the newest point, however it doesn't seem fluid... it kinda just jumps. I'm sure in the past I've read that fluid animations are a simple turn on / turn off affair, I've just not been able to find out how to do this yet!

Once again, thanks so much for your help - and any further advice would be the icing on the cake haha Very Happy


Space for rent. Contact me for rates!
Back to top
garus
VIP Member



Posts: 34200

PostPosted: Fri, 30th Dec 2011 17:33    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:28; edited 1 time in total
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Fri, 30th Dec 2011 18:03    Post subject:
Ok, so thanks for that nudge. I also did a bit of searching online and found this thread:

http://stackoverflow.com/questions/1556921/google-map-api-v3-set-bounds-and-center

which suggested I create a "bounds" variable, and then add to it with every iteration of markers[i].

This is my new code:

Quote:

function initialize() {
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(51.5, -0.1Cool,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var bounds = new google.maps.LatLngBounds();

// Add each marker
for (var i = 0; i < companies.length; i++) {
var company = companies[i];

markers[i] = new google.maps.Marker({
position: new google.maps.LatLng(company.Latitude, company.Longitude),
title: company.Company,
map: map
bounds: extend(myLatLng);
});

map.fitBounds(bounds);

// Don't forget to attach markerClick() to each of them
google.maps.event.addListener(markers[i], 'click', markerClick);
}
}


However, this doesn't work - doesn't load the map:

http://datanalyst.co.uk/dev/flmap/mapexample7d.php

In the example provided on the above website, they suggested extending bounds with "bounds.extend(myLatLng);" - of course, I am using "bounds: extend(myLatLng);" instead - is this correct?


Space for rent. Contact me for rates!
Back to top
garus
VIP Member



Posts: 34200

PostPosted: Mon, 2nd Jan 2012 17:35    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:27; edited 1 time in total
Back to top
Horrordee
Soderator



Posts: 8849
Location: England
PostPosted: Tue, 10th Jan 2012 12:56    Post subject:
I didn't ignore it, I tried my best to use it but I failed.

I got there in the end though, so thanks so much to everyone for their help!!


Space for rent. Contact me for rates!
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group