FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


GOOGLE MAP: check nearest store where you located





macky
hi guys im wondering how can this be done. do you have any idea how to manipulate google map to possibly work with something like this?

for example, i have set of address on my database. Then i want it to fetch from database then passed it to google map then detect where you are, computing approximately what's the distance from where i am up to the given addresses

Thanks in advance.. it would help my thesis.. Smile
SonLight
Yes, I'm pretty sure you can do what you want with the Google api. Here is a link to a tutorial describing it:

http://code.google.com/apis/maps/articles/phpsqlsearch.html

The example code uses its own computation of distance based on the latitude and longitude given. If you need more help with the geolocation api to get the lat/lon info, or if this isn't exactly what you had in mind, let us know. I think a lot of people on Frihost would be interested in hearing more about your thesis when you get it done or make substantial progress on it.
macky
yeah i already read that tutorial about it. I ultimately follow the instructions carefully but the thing is it always says that

address not found even if i almost type the given figures in the database.. i don't what it accepts... the message always

on the side panel of the map... i almost even tried to debug it but unfortunately i don't know why it says as always address

not found.
SonLight
OK, if that tutorial seems to be close to what you're looking for, then it's good to at least understand it, even if you change a lot of the details. I'm assuming you have a Frihost website, or another web server that you're accessing remotely. If you're running on your own computer, you will need to set it up as a web server with php and a database, probably mysql is a good choice.

Assuming you have a server and are able to run php commands successfully in your browser, let us know exactly what command or url is failing. You might want to post a section of the php file showing the command that's failing. If it is failing to access a url that you type into your browser, there may be a setup problem of some sort. Let us know what's happening, and if I can't figure it out there's lots of people here smarter than me about website code.
macky
Code:

//test.html (upper part of the js function)
//this is only the problem where i think i have problem

function searchLocationsNear(center) {
     var radius = document.getElementById('radiusSelect').value;

     //this is the start of the problem where i have encounter
    //even if you manually put that query on url bar of the page where fetch records and generate xml,
    //it returns the expected output but when you tried to connect it to the test.html nothing returns..
     //?lat=37&lng=-122&radius=25
     //alert("lat "+center.lat() + "and the lng "+ center.lng());

     var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;

     //var searchUrl = 'phpsqlsearch_genxml.php?lat=37&lng=-122&radius=25';
     //this is the end of the problem


     GDownloadUrl(searchUrl, function(data) {
       var xml = GXml.parse(data);
       var markers = xml.documentElement.getElementsByTagName('markers');
       map.clearOverlays();

       var sidebar = document.getElementById('sidebar');
       sidebar.innerHTML = '';

//this is were says that if markers echo back to the point that it is nothing then it will says no result found
//but the main problem with this, even i tried to debug it, no wonder, it returns the markers from database
//unfortunately this length always says that length is 0

       if (markers.length == 0) {
         sidebar.innerHTML = 'No results found.';
         map.setCenter(new GLatLng(40, -100), 4);
         return;
       }

       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
         var name = markers[i].getAttribute('name');
         var address = markers[i].getAttribute('address');
         var distance = parseFloat(markers[i].getAttribute('distance'));
         var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));

         var marker = createMarker(point, name, address);
         map.addOverlay(marker);
         var sidebarEntry = createSidebarEntry(marker, name, address, distance);
         sidebar.appendChild(sidebarEntry);
         bounds.extend(point);
       }
       map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     });
   }

//----------------------------------------------------------------
//this is the php part
//let say this is phpsqlsearch_genxml.php that generate the xml which access the test.html

<?php
require("phpsqlsearch_dbinfo.php");

// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect ("localhost", $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));
$result = mysql_query($query);

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

echo $dom->saveXML();
?>



as it says there the main problem is that even if you set the latitude and longitude as static it always return negative result...

to whom that can help me, even a package of script of yours will do... please help guys... i want to make a thesis that others can amazing.. im doing so much my best up to the extent of my limit... i not that good but i want to achieve this feature on my thesis..
macky
YYYYYEEEEEEEEEEEEEEEEEEEEEEEEEEHHHHHAAAAAAAAAAAAAA!!!!!!!!!

i figure out what's the problem.. it is not about the link or what ever that i pass through, it is base on the xml problem..

when i validate if the length of the markers, it is given that space...

example of working xml:
Code:

1 <?xml version="1.0"?>


example of non working xml:
Code:

1
2 <?xml version="1.0"?>


thank god i figure it out right away...
SonLight
I'm glad to hear you got it working. If I understand you right, the only problem was the xml file had an empty line at the beginning, and it couldn't handle that.

Normally one would expect a parser to be able to accept extra blanks or blank lines, and some kind of comment also. Apparently some xml parsers cannot.

Let us know if other problems arise. I look forward to hearing more about the project when you are ready to share it.
Related topics
How to add a Google Map to any web pages!
th 2038 bug and google
StatsCounter New: Recent Visitors on a Google Map
Traffic Reporting System
Pin - map your location on the world map
how to increase google adsense income
Design a Map With Link to each Country...
Heading to San Francisco?
Google Map
How To Print Maps & Take Google Maps Offline
gis application dev. for rain,river,flood
Google-Map (Location) in contain ?
Google Maps Help Needed
Frihost server location
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.