Jump to content

Recommended Posts

need a map which locates all the stores addresses(Less than 10). Those address must be retreived from the database. But the database don't contain any lat or long field. Therefore geocoding an address into their corresponding lat and long coordinates must be done and appropriate markers must be placed.

The code which I have at the moment is as follows:-

<?php
require_once '../model/stores.php';
$obj=new Stores();
$result=$obj->getStores();
$new_array = array();

while($row=mysql_fetch_assoc($result)){
$new_array[] = $row['stores_address']; 
}
$add_js = json_encode( $new_array );
?>

<html>
<head>
<style>
#map_canvas {
width: 500px;
height: 500px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

<script>
$(document).ready(function () {
var map;
var elevator;
var myOptions = {
zoom: 1,
center: new google.maps.LatLng(0, 0),
mapTypeId: 'terrain'
};
map = new google.maps.Map($('#map_canvas')[0], myOptions);

//var addresses = ['Norway', 'Africa', 'Asia','North America','South America'];
var addresses = <?php echo $add_js ?>;;

for (var x = 0; x < addresses.length; x++) {
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json? address=' +addresses[x]+'&sensor=false', null, function (data) {
var p = data.results[0].geometry.location
var latlng = new google.maps.LatLng(p.lat, p.lng);
new google.maps.Marker({
position: latlng,
map: map
});
});
}

});
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>

I have looked on various forums and other web resources but no use as none of them worked out for me...Hope any genius would help me to solve the problem.

Edited by Naseem

What you'll want to use is Google Maps API's Geocoder service. Have a look at the documentation for the details. It's usage is fairly simple:

var request = {address: 'Your address here'};
var coder = new google.maps.Geocoder();
coder.geocode(request, function(result, status){
   if (status == google.maps.GeocoderStatus.OK){
      var location = result.geometry.location;
      //create marker using location.
   }
});
If your list of places is fairly static, you should probably consider just manually obtaining the proper latitude and longitude and save the result to your database as additional columns rather than geocode it each request. You then will not have to worry about google blocking your geocode requests and the map will load significantly faster.

 

I've moved this thread to the Javascript forum since it is primarily a Javascript question and not a PHP question.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.