0

I have working on Core PHP,I have created google maps marker for single static Address, I need pass dynamic multiple name and address from database where will write query getting data from database,single address is working fine but i need multiple address and multiple google markers, how to do this please help me.

Here my code:index.php

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
 <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
    <meta charset="utf-8">
    <style>
      #map {
        height: 100%;
      }
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
  </body>
  <script type="text/javascript">
  var map;
  //alert(map);
  function getData() {
    $.ajax({
    url: "map_api.php",
    async: true,
    dataType: 'json',
    success: function (data) {
      console.log(data);
      init_map(data);
    }
  });  
  }
  function init_map(data) {
        var map_options = {
            zoom: 14,
            center: new google.maps.LatLng(data['latitude'], data['longitude'])
          }
        map = new google.maps.Map(document.getElementById("map"), map_options);
       marker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng(data['latitude'], data['longitude'])
        });
        infowindow = new google.maps.InfoWindow({
            content: data['formatted_address']
        });
        google.maps.event.addListener(marker, "click", function () {
            infowindow.open(map, marker);
        });
        infowindow.open(map, marker);
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC3cIb3aYyySipxodqNJgRDUpIC17VrnXY&callback=getData" async defer></script>
</html> 

Here my map_api.php file:

<?php
function get_geocode($address){
    $address = urlencode($address);
        $url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
        $resp_json = file_get_contents($url);
        $resp = json_decode($resp_json, true);
    //response status will be 'OK'means if able to geocode given address
    if($resp['status']=='OK'){
    $data_arr = array(); 
        $data_arr['latitude'] = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] :'';
        $data_arr['longitude'] = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : '';
        $data_arr['formatted_address'] = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] :'';        
        if(!empty($data_arr) && !empty($data_arr['latitude']) && !empty($data_arr['longitude'])){
            return $data_arr;
        }else{
            return false;
        }
    }else{
        return false;
    }
}
    echo json_encode(get_geocode('4102/4C, SRK Arcade, Tavarkere Main Road, BTM 1st Stage,Bengaluru, Karnataka 560029'));
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
lamp
  • 43
  • 4
  • https://stackoverflow.com/a/3059129/645225 – Kyle Domingo Mar 13 '18 at 06:53
  • i'm not using (lat & long) i need map marker based on address from database – lamp Mar 13 '18 at 07:00
  • Any one check it my code – lamp Mar 13 '18 at 07:32
  • Your `get_geocode(address)` only accepts 1 address to geocode, you are going to have to make a request (to get_geocode) for each address. – 2pha Mar 13 '18 at 07:55
  • You say you're not using lat and long yet in your code: `position: new google.maps.LatLng(data['latitude'], data['longitude'])` – Kyle Domingo Mar 13 '18 at 07:57
  • You can request using ajax multiple times in parallel. Bring the addresses to the webpage and pass to the backend api using ajax. This will save time instead of requesting synchronously from PHP – Kyle Domingo Mar 13 '18 at 07:59

0 Answers0