Displaying google maps pinpoints near a certain specified location
I have a table with some locations. For example:
ID LAT LNG
1 40.759139 -73.979723
I am trying to get the user country and city from his IP address (let's say I have it and it's Germany, Berlin for example).
And then go over all the locations in my DB and select the ones that are near, or within that city (within the city is better) and display them on a map this way: http://code.google.com/apis/maps/documentation/javascript/examples/event-closure.html
Now i've seen this question: how do i find near by locations to a given location using google map api
Which is not really what i'm looking for, although it's close.
Anyone knows how I can accomplish what i'm trying to do?
Th开发者_运维百科anks,
Figured it out, this is how you do it:
Note: $default holds the city lat/lng that you would like to initially display. mapEvents() returns an array with all lat/lng/title of the event.
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(<?= $default ?>);
var myOptions = {
zoom: 9,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php
$pinpoints = mapEvents();
foreach ($pinpoints as $pinpoint) {
$loc = $pinpoint['UA_LAT'] . "," . $pinpoint['UA_LNG'];
echo("
var myLatlng = new google.maps.LatLng(" . $loc . ");
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: '" . $pinpoint['EVENT_NAME'] . "'
});
");
$loc = "";
}
?>
}
</script>
精彩评论