apneadiving / Google-Maps-for-Rails

Enables easy Google map + overlays creation in Ruby apps
https://apneadiving.github.io/
MIT License
2.26k stars 382 forks source link

get user's location & pass to controller #195

Closed tknazn closed 12 years ago

tknazn commented 12 years ago

I follow your instruction and below is my ajax call. Hope you can help me to figure this out. The map is still loaded but none of item shows up. https://github.com/apneadiving/Google-Maps-for-Rails/issues/184

-------- items/index.html.erb -------------

<%= gmaps("markers" => {"data" => @json},   
    "map_options" => {"auto_zoom" => true, "detect_location" => true, 
        "center_on_user" => true, "zoom" => 13, "auto_adjust" => true })%>

<script type="text/javascript">
    $(document).ready(function(){
        //var lat = Gmaps.map.userLocation.lat();
        //var log = Gmaps.map.userLocation.lng();
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        setInterval( function(){
            $.ajax({
                type: 'POST',
                url: '/yells/index',
                data: {userLat: lat, userLng: log },
                dataType: 'json'
                });
            },10000);
        });
</script>

-------- ItemController ------ def index

@userLat = params[:userLat]
@userLng = params[:userLng]

if params[:search].present?
  @yells = Item.search(params[:search])
  @json = Item.search(params[:search]).to_gmaps4rails
else

  @json = Item.near([@userLat,@userLng],10).to_gmaps4rails
  @yells = Item.all
end

respond_to do |format|
  format.html # index.html.erb
  format.json { render :json => @yells }
end

end

apneadiving commented 12 years ago

Obviously this will spit a js error.

Where did you find 'instructions' like this?

Sent from my iPhone

On 24 avr. 2012, at 08:08, tknaznreply@reply.github.com wrote:

I follow your instruction and below is my ajax call. Hope you can help me to figure this out. The map is still loaded but none of item shows up. https://github.com/apneadiving/Google-Maps-for-Rails/issues/184

-------- items/index.html.erb -------------

<%= gmaps("markers" => {"data" => @json},
"map_options" => {"auto_zoom" => true, "detect_location" => true, "center_on_user" => true, "zoom" => 13, "auto_adjust" => true })%>

-------- ItemController ------ def index

@userLat = params[:userLat] @userLng = params[:userLng]

if params[:search].present? @yells = Item.search(params[:search]) @json = Item.search(params[:search]).to_gmaps4rails else

 @json = Item.near([@userLat,@userLng],10).to_gmaps4rails
 @yells = Item.all

end

respond_to do |format| format.html # index.html.erb format.json { render :json => @yells } end

end


Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/195

apneadiving commented 12 years ago

Some pseudo code:

<%= gmaps(...) %>

Sent from my iPhone

On 24 avr. 2012, at 08:08, tknaznreply@reply.github.com wrote:

I follow your instruction and below is my ajax call. Hope you can help me to figure this out. The map is still loaded but none of item shows up. https://github.com/apneadiving/Google-Maps-for-Rails/issues/184

-------- items/index.html.erb -------------

<%= gmaps("markers" => {"data" => @json},
"map_options" => {"auto_zoom" => true, "detect_location" => true, "center_on_user" => true, "zoom" => 13, "auto_adjust" => true })%>

-------- ItemController ------ def index

@userLat = params[:userLat] @userLng = params[:userLng]

if params[:search].present? @yells = Item.search(params[:search]) @json = Item.search(params[:search]).to_gmaps4rails else

 @json = Item.near([@userLat,@userLng],10).to_gmaps4rails
 @yells = Item.all

end

respond_to do |format| format.html # index.html.erb format.json { render :json => @yells } end

end


Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/195

tknazn commented 12 years ago

Can't thank you enough for your help. Ive been picking up learning it lately, found help from several sites.. but your code really makes sense to me. Oddly, the problem still exists. Can you shed a light on this?

<script type="text/javascript">

    function checkAndSendUserlocation(){

         if (Gmaps.map.userlocation !== null)

             {
                $.ajax({

                          type: 'POST',
                           url: '/yells',
                          data: {userLat: Gmaps.map.userLocation.lat(), userLng: Gmaps.map.userLocation.lng() },
                          dataType: 'html'

                });
             }

            else {
               setTimeout(checkAndSendUserLocation, 2000);
                }
            }

        Gmaps.map.callback = function() {
         checkAndSendUserlocation();
        }

    </script>
------- route.rb ------
resources :yells
root :to => 'yells#index'
match '/yells' => 'yells#index', :via => :post
---- application.html.erb ----
...
<%= javascript_include_tag "application" %>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  <%= csrf_meta_tags %>
apneadiving commented 12 years ago

What's the error you've got?

Why do you pass dataType: 'html' in ajax request?

Sent from my iPhone

On 24 avr. 2012, at 09:52, tknaznreply@reply.github.com wrote:

Can't thank you enough for your help. Ive been picking up learning it lately, found help from several sites.. but your code really makes sense to me. Oddly, the problem still exists. Can you shed a light on this?

------- route.rb ------ resources :yells root :to => 'yells#index' match '/yells' => 'yells#index', :via => :post

---- application.html.erb ----

... <%= javascript_include_tag "application" %>

<%= csrf_meta_tags %>


Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/195#issuecomment-5299306

tknazn commented 12 years ago

I did some debugging. Seems like they didn't get through to the controller. I also tried to use datatype: json or script but it still doesn't work. Do you think it has anything to do with route.rb?

<%= debug @userLat %>
<%= debug @userLng %>

----Result ----
...
---
...
--- 
apneadiving commented 12 years ago

How could I know that?

You should dive in your js debugger to get the blocking point.

tknazn commented 12 years ago

Updated:

I did some debugging using firebug and found out this error:

Gmaps.map.userLocation is null 

That caused the script broken @

data: {userLat: Gmaps.map.userLocation.lat(), userLng: Gmaps.map.userLocation.lng() },

But then after page is loaded completely and I was able to see the value of Gmaps.map.userLocation thru watch/breakpoints. I thought the setInterval handles that issue. Am I missing any other exceptions?

apneadiving commented 12 years ago

Ok.

That's because you have to put your js code:

Sent from my iPhone

On 25 avr. 2012, at 05:11, tknaznreply@reply.github.com wrote:

I did some debugging using firebug and found out this error: Gmaps.map is undefined

<script type="text/javascript">
.........
Gmaps.map.map.callback = function() {              #this where the issue locates at
checkAndSendUserlocation();
}
</script>
</div> 

Reply to this email directly or view it on GitHub: https://github.com/apneadiving/Google-Maps-for-Rails/issues/195#issuecomment-5322828

tknazn commented 12 years ago

I also thought that was the reason and changed but that wasn't the core issue. I found two issues (second one keep repeating .. I think it is because setTimeout function)

Error #1: This is was from the line: data: {userLat: Gmaps.map.userLocation.lat(), userLng: Gmaps.map.userLocation.lng() },

Uncaught TypeError: Cannot call method 'lat' of null

Error #2 (multiple times): This came from the application.js -- jQuery JavaScript Library v1.7.2. Incompatible with v.1.7.2?

Uncaught ReferenceError: position is not defined
(anonymous function)

yells/index.html.erb

.......
    <%= gmaps("markers" => {"data" => @json},   
        "map_options" => {"detect_location" => true, 
            "center_on_user" => true, "zoom" => 13 })%>

    <% content_for :scripts do %>
    <script type="text/javascript" charset="utf-8">

        Gmaps.map.callback = function() {
         checkAndSendUserlocation();
        }

        function checkAndSendUserlocation(){
            var userLat = 1;
            var userLng = 1;

             if (Gmaps.map.userlocation !== null)
             {
                $.ajax({
                    type: 'POST',
                    url: '/yells/index',
                    data: {userLat: Gmaps.map.userLocation.lat(), userLng: Gmaps.map.userLocation.lng() },
                    dataType: 'json',
                });
             }
            else {
             setTimeout(checkAndSendUserLocation, 2000);
                }
            }
    </script>
    <% end %>
......
apneadiving commented 12 years ago

it's a typo:

Gmaps.map.userlocation !== null

instead of:

Gmaps.map.userLocation !== null
tknazn commented 12 years ago

I think it need to be reversed. Gmaps.map.userLocation !== null

but it works now. I appreciate your great help