google maps sencha touch 2 configuration - google-maps

Hi I am implementing google maps in sencha touch as suggested here:
google maps implementation in sencha touch 2 (the MVC way)
However, when the map appears, it first displays a default location (somewhere in United States) and then re-renders again to show the map according to my configuration. How can I avoid this?
Ext.define('App.view.Map', {
extend: 'Ext.Map',
xtype: 'map',
useCurrentLocation: false,
config: {
layout: 'fit',
iconCls: 'icon-location',
title: 'Location',
styleHtmlContent: true,
items: {
docked: 'top',
xtype: 'titlebar',
title: 'Location'
}
},
mapOptions: {
center: new google.maps.LatLng(<value>, <value>),
disableDefaultUI: true
},
constructor: function(config) {
this.callParent(config);
if (!(window.google || {}).maps) {
this.setHtml('<p id="maperror">Internet Connection Required!</p>');
}
}
});

You defined map view and extended Ext.Map, so that view becomes map and when you give xtype to your view, It should not be predefined xtype like map, panel, button, etc..
You should learn Ext class system and try this code.
Ext.define('myapp.view.Map', {
extend: 'Ext.Map',
xtype: 'mymap',
config: {
layout: 'fit',
iconCls: 'icon-location',
title: 'Location',
useCurrentLocation: false,
styleHtmlContent: true,
items: [{
docked: 'top',
xtype: 'titlebar',
title: 'Location'
}],
mapOptions: {
center: new google.maps.LatLng(<value>, <value>),
disableDefaultUI: true
}
},
constructor: function(config) {
this.callParent(config);
if (!(window.google || {}).maps) {
this.setHtml('<p id="maperror">Internet Connection Required!</p>');
}
}
});

You can set the starting location by adding a "center" option to the map's "mapOptions" config.
{
xtype: 'map',
mapOptions: {
center: new google.maps.LatLng(-34.397, 150.644)
}
}
You can just override the Ext.Map class and add your own message.
Ext.define('MyApp.overrides.Map', {
constructor: function() {
this.callParent(arguments);
// this.element.setVisibilityMode(Ext.Element.OFFSETS);
if (!(window.google || {}).maps) {
// do your own thing here
}
}
});

Related

How to retrieve data from a store in Sencha Touch

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?
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.

Google map handling in sencha

I have implemented the map functionality in my sencha based application. The map is showing but cant able to zoom in or zoom out & can't be able to move in other direction. It showa like static in the image.
But I want to show the map as like the following
Here is my code
var mapapnel = Ext.create('Ext.Panel', {
id: 'mapapnel',
layout:'card',
height: '100%',
width: '100%',
items: [
{
xtype: 'toolbar',
ui:'light',
docked: 'top',
title: 'Find location',
items: [{
text: 'Back',
ui: 'back',
handler: function() {
Ext.getCmp('homepnl').setActiveItem(1);
}
},{
xtype:'spacer'
}
]
},
{
xtype:'map',
useCurrentLocation:false,
mapOptions: {
zoom: 15,
zoomControl : true,
center: new google.maps.LatLng(11.0183, 76.9725),
mapTypeId: google.maps.MapTypeId.ROADMAP,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
}
}
}
]
})
what change required in my code add the functionalites zoom in & zoom out.
Here my answer one more time:
It look likes a layout problem, the map should be inside a container with layout 'fit'.

Google maps API required

When i try to show the map in my aplication its showing the messsage as "Google maps API required" in the view, Here my code
var mapapnel = Ext.create('Ext.Panel', {
id: 'mapapnel',
height: '100%',
width: '100%',
fullscreen: true,
layout:'fit',
layout:'vbox',
items: [{
xtype: 'toolbar',
ui:'light',
docked: 'top',
title: 'Find location',
items: [{
text: 'Back',
ui: 'back',
handler: function() {
Ext.getCmp('homepnl').setActiveItem(1);
}
},{
xtype:'spacer'
}]},
{
xtype:'map',
useCurrentLocation:true
}]});
What change required to show the map?please help me to solve
You need to load the google maps javascript api.
you can do this by adding the code in your index.html file.
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
Two things
1) The panel which contains map should be card layout.
2) Without setting center mapOptions, map not showing.
Give Lat & Lng value as you need.
var mapapnel = Ext.create('Ext.Panel', {
id: 'mapapnel',
layout:'card',
items: [{
xtype: 'toolbar',
ui:'light',
docked: 'top',
title: 'Find location',
items: [{
text: 'Back',
ui: 'back',
handler: function() {
Ext.getCmp('homepnl').setActiveItem(1);
}
}
]},
{
xtype:'map',
useCurrentLocation:true,
mapOptions: {
zoom: 12,
zoomControl : true,
center: new google.maps.LatLng(Lat, Lng),
disableDefaultUI: true
}]
});

Sencha touch map center

I've created a new sencha app with the sencha-cmd. I'm a bit new to this. So i've tried to create a map:
{
title: 'Map',
iconCls: 'locate',
layout: 'card',
useCurrentLocation: false,
fullscreen: false,
id: 'locationmap',
mapOptions: {
zoom: 12,
center : new google.maps.LatLng(37.381592, -122.135672),
navigationControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.DEFAULT
}
},
items: [
{
docked: 'top',
xtype: 'titlebar',
title: 'Getting Started'
},
{
xtype: 'map'
}
]
}
I've included the necessary google maps api files, and also required Ext.Map
However the map isn't centering on 37.381592, -122.135672, its centering elsewhere. How can I fix this?
Additionally, how can I access the maps object? So I can call stuff like .getCenter()
Not sure on the centering, but you could do it after the fact with:
theView.setMapCenter({latitude: 37.381592, longitude: -122.135672});
As for getting the element, you can simply call:
theView.getMap();
This is all coming from the Sencha Touch 2 docs, FYI. I'm interested to see how it works out, I'll be implementing a map in my new app soon.
EDIT:
Looking at it more closely, shouldn't your mapOptions parameter be on the xtype: 'map' item?
...
{
xtype: 'map',
useCurrentLocation: false,
mapOptions: {
zoom: 12,
center : new google.maps.LatLng(37.381592, -122.135672),
navigationControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.DEFAULT
}
}
}
...

Sencha Touch 2: Google Maps Directions Route won't show

I'm using a view to show a location on a map with a small form below it to grab the users address if they want directions. The map renders initially as I want. There is a controller to handle tapping the button and updating the display with the route. I can see that it is successfully retrieving the route information. It's just not updating the display to show it. What am I missing?
Here's the view:
var tcmlatlng = new google.maps.LatLng(38.898748, -77.037684);
Ext.define('VisitTCMIndy.view.Directions',{
extend: 'Ext.Panel',
requires: [
'Ext.form.Panel',
'Ext.Map'
],
config: {
layout: 'vbox',
items: [
{
xtype: 'map',
useCurrentLocation: false,
flex: 3,
mapOptions: {
center: tcmlatlng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
listeners: {
maprender: function(map,gmap,options){
var homemarker = new google.maps.Marker({
position: tcmlatlng,
map: gmap
});
}
}
},
{
xtype: 'formpanel',
id: 'directionsform',
flex: 1,
items:[
{
xtype: 'textfield',
name: 'address',
label: 'Address',
},
{
xtype: 'button',
ui:'action',
text: 'Get Directions'
}
]
}
]
}
});
Here's the controller
Ext.define('VisitTCMIndy.controller.Directions',{
extend: 'Ext.app.Controller',
config: {
control: {
dButton: {
tap: 'loaddirections'
}
},
refs: {
dButton: '#directionsform button[ui=action]',
tcmmap:'map',
addfield:'textfield[name=address]'
}
},
loaddirections: function(dbutton){
console.log('loaddirections');
var gmap = this.getTcmmap();
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
directionsDisplay.setMap(gmap.map);
var tcmadd = "1600 Pennsylvania Ave, Washington, DC";
var originadd = this.getAddfield().getValue();
var request = {
origin: originadd,
destination: tcmadd,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status){
console.log(status);
if(status = google.maps.DirectionsStatus.OK){
console.log(result);
directionsDisplay.setDirections(result);
}
});
}
});
I was referencing the map incorrectly. I was trying to reference it directly instead of using the getter. So anywhere you see 'gmap.map' it should read 'gmap.getMap()'.