Re: [jgen] Component development issues

73 views
Skip to first unread message

Allon Moritz

unread,
Apr 8, 2013, 7:57:28 AM4/8/13
to joomla-de...@googlegroups.com
You need a combination of both. Use javascript to make the ajax request and get latitude and longitude for your address from a php script and display it in the map.

To get the lat/long in a controller action:
$http = new JHttp();
JFactory::getApplication()->close();

and in javasscript something like:
jQuery.ajax({
type: 'post',
url: 'index.php?option=com_my&addr='+jQuery('location').val(),
data: data,
success: function (response) {
var json = jQuery.parseJSON(response);
var map = new google.maps.Map(jQuery('#map').get(0), {zoom: 4, mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(20, 20)});

var l = new google.maps.LatLng(json.latitude, json.longitude);
var marker = new google.maps.Marker({position: l, map: map});
var infowindow = new google.maps.InfoWindow({content: 'desc});
google.maps.event.addListener(marker, 'click', function() {infowindow.open(map, marker);});
dpcalendarMapBounds.extend(l);
dpcalendarMap.setCenter(dpcalendarMapBounds.getCenter());
}
});

this code is not tested and will not work right from the beginning...you need to debug it...but it should give you a starting point...


On Mon, Apr 8, 2013 at 12:34 PM, Rebeca Nenciu <rebeca...@gmail.com> wrote:
Hello,

I am developing a component for Joomla which should:

1. Show form (name,telephone, address, etc.) on frontend
2. Upon form completion by site visitors, on form button click :show gmap focused on visitor's address.
3. Visitor puts marker on gmap to point his home's rooftop (component registers in post variable lat and lng for point + all other fields)
4. On submit button after selecting rooftop all data is inserted into component's table, an account for the visitor is created (users table)+ mail etc etc and that account is linked to his entry in the component's table+ a row in another table also linked to the component's table. Afterwards all this will be synchronized with a filemaker database via an odbc driver.
I am developing this component for my bachelor's degree exam and for a small company. However I am new to Joomla and to php so I have a rough time getting a hold of it and it's MVC pattern. Eg of the questions I have in mind: Should I use javascript direct or a php class for the map? If someone can spare one or two hours some of these days for helping me check what I did well and what wrong and maybe coach me a bit I would be grateful.
Becky

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.



Rebeca Nenciu

unread,
Apr 9, 2013, 8:33:13 AM4/9/13
to joomla-de...@googlegroups.com
I don't get this ajax thing.So when i click my button i should call first the javascript function..which should be placed in assets js and then pass to my controller..? and then store all the data eg (name, coordinates etc) using controller and model? sorry i am bothering you but i somehow got stuck 

Mark Dexter

unread,
Apr 9, 2013, 11:08:47 AM4/9/13
to joomla-de...@googlegroups.com
I found AJAX very confusing when I started looking at it. One thing
that really confused me was code like the following example (from
administrator/components/com_finder/controllers/indexer.json.php):

// Send the JSON response.
echo json_encode($response);

// Close the application.
JFactory::getApplication()->close();

The key to understanding this code (at least for me) was to understand
that this PHP program was being called from a JavaScript program. The
JS program is waiting for a JSON encoded string to be echo'd and then
the JS program will read that string and continue it's processing
after the PHP process closes.

I still find the ways in which JS and PHP interact to be a bit
mysterious and confusing, but at least this helped me understand it a
bit better.

If you look at the Joomla installation, which uses AJAX, or this
example from com_finder, and carefully trace through the
back-and-forth between PHP and JavaScript, it might help you.

Good luck. Mark

Rebeca Nenciu

unread,
Apr 10, 2013, 1:43:56 PM4/10/13
to joomla-de...@googlegroups.com
hey mark, thanks for drawing my attention on encoding the response. I still got problems on making it work. I somehow can't get the field values in my controller.

my script:


jQuery.noConflict();
jQuery(document).ready(function($){
jQuery('#lb').click(function(){ 
$.ajax({
       type: 'POST',
   
       cache:false,
       dataType:"json",
       error: function(xhr, status, error) {
        alert(error);
           alert(status);
           alert(xhr.responseText);
       },
       success: function(response) { 
        alert(response['latitude']);
       
        $('#content').html("<h1>"+response['latitude']+"<br>"+response['longitude']+"</h1>");
     
              }
   });
});
my controller:

<?php
defined('_JEXEC') or die;

require_once JPATH_COMPONENT.'/controller.php';
jimport('joomla.application.component.controllerform');

class HelloWorldControllerInscription extends JControllerForm
{
public function test(){
$jform = JRequest::getVar('jform');
$addresse    = trim(urlencode($jform['addresse']));
$ville       = trim(urlencode($jform['ville']));
$region      = trim(urlencode($jform['region']));
$pays    = 'France';
$cp       = trim(urlencode($jform['codepostal']));
$location=$addresse.',+'.$ville.',+'.$region.',+'.$pays;
$output     = json_decode($geocode); //Store values in variable
if($output->status == 'OK'){ // Check if address is available or not
$array=array("latitude"=> $output->results[0]->geometry->location->lat,"longitude"=> $output->results[0]->geometry->location->lng);
echo json_encode($array);
}
else {
echo json_encode("Sorry we can't find this location.Check the details again!");
}
}
}

So when i run this i get the coordinates for france. This one of the many versions i have on my pc.i have another version in which i try to assign the input from the form in jquery(it gets there )but then i can t get it to be read in the controller . I feel i m getting close and i just don t get where i am wrong. I should finish this small part of the project today and start writing some of my theory for my paper so if you have any lead to get me back on track it would help me a lot. Becky

Mark Dexter

unread,
Apr 10, 2013, 2:29:07 PM4/10/13
to joomla-de...@googlegroups.com
Hi. One important trick is to learn how to debug the Javascript. You
can use either Chrome or FF with Firebug. If you look at the console
in either debugger it will show you if you have any JS errors. You can
also find out if your JS is not being executed at all. Take a look at
that and see if you can make any headway. Good luck! Mark

P.S. You can also look at my repo here
https://github.com/dextercowley/bug-squad-stuff/tree/version3-0 and
see the AJAX work I did for the Bug Squad charts using JS. That might
give you some idea. Check out components/com_trackerstats.
Reply all
Reply to author
Forward
0 new messages