⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Store latitude longitude to database



sirsanndy
  • profile picture
  • Member

Posted 27 June 2015 - 05:24 AM

Hi !

I've this example code :

<html>
<body onLoad="initialize()">

  <div id="map_canvas" style="width:50%; height:50%"></div>

  <div id="latlong">
    <p>Latitude: <input size="20" type="text" id="latbox" name="lat" ></p>
    <p>Longitude: <input size="20" type="text" id="lngbox" name="lng" ></p>
  </div>

</body>
</html>

<cfoutput>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=#YOUR-GOOGLE-API-KEY#&sensor=false"></script>
</cfoutput>

<script type="text/javascript">
//<![CDATA[

    // global "map" variable
    var map = null;
    var marker = null;

    // popup window for pin, if in use
    var infowindow = new google.maps.InfoWindow({ 
        size: new google.maps.Size(150,50)
        });

    // A function to create the marker and set up the event window function 
    function createMarker(latlng, name, html) {

    var contentString = html;

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        zIndex: Math.round(latlng.lat()*-100000)<<5
        });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString); 
        infowindow.open(map,marker);
        });

    google.maps.event.trigger(marker, 'click');    
    return marker;

}

function initialize() {

    // the location of the initial pin
    var myLatlng = new google.maps.LatLng(33.926315,-118.312805);

    // create the map
    var myOptions = {
        zoom: 19,
        center: myLatlng,
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

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

    // establish the initial marker/pin
    var image = '/images/googlepins/pin2.png';  
    marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      icon: image,
      title:"Property Location"
    });

    // establish the initial div form fields
    formlat = document.getElementById("latbox").value = myLatlng.lat();
    formlng = document.getElementById("lngbox").value = myLatlng.lng();

    // close popup window
    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
        });

    // removing old markers/pins
    google.maps.event.addListener(map, 'click', function(event) {
        //call function to create marker
         if (marker) {
            marker.setMap(null);
            marker = null;
         }

        // Information for popup window if you so chose to have one
        /*
         marker = createMarker(event.latLng, "name", "<b>Location</b><br>"+event.latLng);
        */

        var image = '/images/googlepins/pin2.png';
        var myLatLng = event.latLng ;
        /*  
        var marker = new google.maps.Marker({
            by removing the 'var' subsquent pin placement removes the old pin icon
        */
        marker = new google.maps.Marker({   
            position: myLatLng,
            map: map,
            icon: image,
            title:"Property Location"
        });

        // populate the form fields with lat & lng 
        formlat = document.getElementById("latbox").value = event.latLng.lat();
        formlng = document.getElementById("lngbox").value = event.latLng.lng();

    });

}
//]]>

</script>

Which I want to do is show maps in latitude and longitude field in grocery CRUD, and automatically fill both fields when I click the map like above html, then store the values to my database.
How to do this ?

 


Paul Savostin
  • profile picture
  • Member

Posted 27 June 2015 - 08:34 AM

Hi! You can do this using callback_field function where you add map div. For lat and lng you can using hidden fields, other do with pure js.


sirsanndy
  • profile picture
  • Member

Posted 28 June 2015 - 05:08 AM

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Usaha extends MY_Controller {

	public function index()
	{
		// CRUD table
		$this->load->helper('crud');
		$this->config->set_item('grocery_crud_file_upload_allow_file_types', 'jpeg|jpg|png');
		$crud = generate_crud('TDataUsaha');
		$crud->columns('Id_usaha', 'nama_usaha', 'produk_utama','alamat','telp','longitude','latitude','gambar','no_ktp','id_sektor','id_skala','id_kelurahan');
		
                //hidden field
		$crud->field_type('longitude', 'hidden', $_GET['longitude']);
                $crud->field_type('latitude', 'hidden', $_GET['latitude']);

		$crud->callback_field('longitude',array($this,'callback_lng'));
                $crud->callback_field('longitude',array($this,'callback_ltd'));		

		$crud->set_field_upload('gambar','assets/images');

		$crud->display_as('no_ktp', 'Nama Pemilik');
		$crud->set_relation('no_ktp', 'TPemilikUsaha', 'nama');

		$crud->display_as('id_sektor', 'Sektor');
		$crud->set_relation('id_sektor', 'TSektor', 'nama_sektor');

		$crud->display_as('id_skala', 'Skala');
		$crud->set_relation('id_skala', 'TSkalaUsaha', 'nama_skala');

		$crud->display_as('id_desa', 'Desa');
		$crud->set_relation('id_desa', 'TDataDesa', 'nama_desa');

		$this->mTitle = "Usaha";
		$this->mViewFile = '_partial/crud';
		$this->mViewData['crud_data'] = $crud->render();
	}
	
	public function callback_lng($value = '', $primary_key = null)
	{
		$this->load->view('');
		return '';
	}

	public function callback_ltd($value = '', $primary_key = null)
	{
		$this->load->view('');
		return '';
	}
}

Hey, thanks for your answered. This my controller. Mmmh, I'm not quite sure what I've to do. Can you give me an example ?

Btw, this is my first project with grocery CRUD and third project with CodeIgniter. So, I need more help in here, please.


Paul Savostin
  • profile picture
  • Member

Posted 28 June 2015 - 12:58 PM

How I can help if you even dont know what you have to do? We here help to implement some specific tasks with GC - not write all code for you!

And you just write some bunch of code and say, "I'm not quite sure what I've to do". Plan you actions, what you need and etc.

When you will have some more real questions - write here and I will help, but now you just waste my time. Sorry


sirsanndy
  • profile picture
  • Member

Posted 28 June 2015 - 14:33 PM

Ahaha calma, sir. I'm still trying to figure out.


Paul Savostin
  • profile picture
  • Member

Posted 28 June 2015 - 16:39 PM

Good. Narrow down your problem and I will help you if I can ;)


Noel Calonia
  • profile picture
  • Member

Posted 16 September 2015 - 09:23 AM

same problem here, i just wondering on how to display map inside add, edit and view pane of groceryCrud using biostall, and i want to save it on the other table. @Paul Savostin, any idea will much appreciated thanks :)