0

Now I have my store: stuStore set up which contains some mock-up data. I have another page which is a Google Map.

Student data structure: {name, geolocation,value, etc....}

I would like to display each of students info based on their locations which is inside of stuStore onto the google Map.

Here is the code:

Ext.define('myapp.view.Main', {
    extend: 'Ext.tab.Panel',
    xtype: 'main',
    fullscreen: true,
    requires: [
        'Ext.TitleBar',
        'Ext.Video',
        'Ext.Map',
        'Ext.Panel',
        'Ext.tab.*',
        'Ext.*'
    ],
    config: {
        tabBarPosition: 'bottom',

        items: [
            {
                title: 'myapp',
                iconCls:'maps',
                xtype: 'map',
                useCurrentLocation: true,
                store: 'myapp.store.stuStore'
            }

How can I do it?

Update 1

var mapdemo = Ext.create('Ext.Map', {
    mapOptions: {
        center: new google.maps.LatLng(-37.73228, 144.86900),  //nearby San Fran
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.DEFAULT
        }
    },
    title: 'MyApp',
    iconCls: 'maps',
    fullscreen: true,
    xtype: 'map',
    id: 'mainMap',
    useCurrentLocation: false,
    listeners: {
        maprender: function (comp, googleMap) {
            var store, latlng, marker;
            store = Ext.getStore('myapp.store.stuStore');

            store.each(function (record, index, length) {
                latlng = new google.maps.LatLng(record.get('Lat'), record.get('Lng'));

                marker = new google.maps.Marker({
                    position: latlng,
                    map: this
                });
            });     
        }

    }
}),
    infowindow = new google.maps.InfoWindow({
        content: 'Sencha HQ'
    });


Ext.define('myapp.view.Main', {
    extend: 'Ext.tab.Panel',
    xtype: 'main',
    fullscreen: true,
    config: {
        tabBarPosition: 'bottom',
        items: [mapdemo],
    }
});

This is my updated code in view\Main.js

But I don't get any markers and it keeps throwing an error:

Uncaught TypeError: Cannot call method 'each' of undefined

Incidentally, the icon on the toolbar which is docked at bottom of the screen is gone as well.

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Franva
  • 6,565
  • 23
  • 79
  • 144

1 Answers1

1

You can create markers based on store records by looping through them in the maprender event callback like this:

Ext.define('myapp.controller.MapController', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            mapComponent: 'main map'
        },
        control: {
            mapComponent: {
                maprender: 'onMaprender',
            }
        }
    },
    onMaprender: function(mapComponent, googleMap) {
        var store, latLng, marker;

        // Get store
        store = Ext.getStore('myapp.store.stuStore')

        // On each store record
        store.each(function(record, index, length) {

            // Get position
            latLng = new google.maps.LatLng(record.get('lat'), record.get('lng'));

            // Create marker
            marker = new google.maps.Marker({
                position: latLng,
                map: googleMap
            });
        });
    }
});

Have a look at this Sencha Fiddle I put together.

M165437
  • 897
  • 1
  • 10
  • 18
  • Hi M165437, thank you so much for your reply!!! really! Do I need to do something like: googleMap.draw(marker); ???? I just saw the marker is initialized but never been used. Thanks again !!! – Franva Apr 06 '14 at 12:43
  • Hi Franva, the marker should be added to the map automatically. I suggest you have a look at the [Sencha Touch map example](http://docs.sencha.com/touch/2.3.1/#!/example/maps), also to be found in the examples folder in your Sencha Touch SDK. Are you looking for your marker at the right position (lat/lng)? – M165437 Apr 06 '14 at 12:59
  • Hi M16, thank you for your advice. I will do it right now and give it a try ASAP and keep you posted. Once again, I highly appreciate your help!!!You are the only one who replied my question. Did I ask the wrong one? Why aren't there any other answers? – Franva Apr 06 '14 at 13:10
  • Hi M16, I saw that Map Example, but there is no code to learn, just a simulator.... – Franva Apr 06 '14 at 13:12
  • oh~~~~NVM, the code is in the example folder. But I cannot run it, because it says that example is based on a older version of SenCha Touch..... – Franva Apr 06 '14 at 13:18
  • Have a look at your local Sencha Touch SDK folder and check the examples. There you'll find the code. – M165437 Apr 06 '14 at 13:18
  • I don't have any problems running the example. Make sure you run it in a local server environment, otherwise the XMLHttpRequest cannot load because of the cross origin policy. – M165437 Apr 06 '14 at 13:34
  • Hi M16, I opened that folder c:\xxx\touch\examples\map then : sencha web start. It shows cannot find resources\css\sencha-touch.css . So I opened that folder and did not find it. So I am thinking may be I need to type some commands like : sencha build? – Franva Apr 06 '14 at 13:43
  • Btw, I did use : sencha app build, but after that the problem persists – Franva Apr 06 '14 at 13:49
  • Unfortunately the examples don't work with sencha web start. You need to run them in a local server environment, e.g. [xampp](https://www.apachefriends.org) or [mamp](http://www.mamp.info/). – M165437 Apr 06 '14 at 13:50
  • Hi M16, I found the reason. when using sencha app build, it generates built examples under the touch\built-examples\testing\Map(map is in my case). So I opened that folder and ran: sencha web start. Bum~~~everything works~~~ haha. I think it's just a kind of protection mechanism. I will try to apply the usable code into my project and to see what difficulties I would encounter and let you know. cheers mate :) – Franva Apr 06 '14 at 14:00
  • Hi M16. I have gone through the example and found it's not MVC friendly(I don't know which parts in the example to put in which part of my MVC). Also, I don't know how to stick controller and view...In your example code, I tried to find something like Main(which is my view) but I didn't. so I don't know how to stitch them together to make them work together. Could you explain it please?? thank you very much! – Franva Apr 06 '14 at 14:18
  • I can't really help you. My answer creates a marker and adds it to the map. Are you sure you're looking at the right position to find your marker? – M165437 Apr 06 '14 at 14:50
  • Sorry if I created some confusion to you. I think I have found how to glue the controller and view, I should follow the naming convention. The only question left here is : mapComponent: 'main map' <-- why it's main map? I thought it should be the name of the instance of the google map on the page, isn't it? – Franva Apr 06 '14 at 15:02
  • 1
    The mapComponent refers to the [Sencha Touch map component](http://docs.sencha.com/touch/2.3.1/#!/api/Ext.Map). It's a wrapper for the google map. 'main map' is like a path to this component: 'main' is the xtype of the view and 'map' the xtype of the Sencha Touch map component. The maprender event fires on the Sencha Touch map component and the callback has two arguments that give you the component and the actual google maps. – M165437 Apr 06 '14 at 15:08
  • Hi M16, thank you for your help. I have followed your advice and instructions from the exampl, many errors have been fixed, but only one is left. Btw, I put all code in view/Main.js because the controller code doesn't get called. Please take a look the update 1 part. thanks !! – Franva Apr 06 '14 at 17:04
  • 1
    I put together this [Sencha Fiddle](https://fiddle.sencha.com/#fiddle/4rb). Have a look! – M165437 Apr 06 '14 at 18:12
  • Oh~~~ M16, you made my day~~~ now everything works like a charm!!!! Thank you very much!!!!! – Franva Apr 07 '14 at 06:22
  • Hi mate, sorry to disturb you from the answered post(I don't know how to chat to you in other places). I now need to retrieve data from a xml file which is on a remote server. Could you please help me out? Here is my question: http://stackoverflow.com/questions/23007336/how-to-retrieve-xml-from-a-remote-server-in-sencha-touch thanks a lot – Franva Apr 11 '14 at 08:27