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

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()'.

Related

The new marker has updated its location but old marker not remove in ionic native google map

The marker is updating to new position and set a new marker but problem with old position marker not remove. marker.remove() not work under this if condition. How I remove old marker? any solution please. here is my code =>
loadMap() {
this.map = GoogleMaps.create('map_canvas', {
controls: {
myLocationButton : true,
myLocation : true
},
camera: {
target: {
lat: this.latitude, lng: this.longitude
},
zoom: 18,
tilt: 30
},
});
this.map.addMarker({
position: { lat: this.latitude, lng: this.longitude },
draggable: true,
disableAutoPan: true,
icon: 'blue',
title: 'Avirup'
}).then((marker: Marker) => {
marker.showInfoWindow();
const subscription = this.geolocation.watchPosition().subscribe(position => {
let geoposition = (position as Geoposition);
let latitude = geoposition.coords.latitude;
let longitude = geoposition.coords.longitude;
let marker : Marker
marker = this.map.addMarkerSync({
position: { lat: latitude, lng: longitude },
draggable: true,
disableAutoPan: true,
icon: { url: './assets/image/addresspin.png' ,
size: {
width: 30 ,
height: 30
}},
title: 'Move'
})
marker.showInfoWindow();
if(marker.getPosition().lat == latitude && marker.getPosition().lng == longitude){
marker.remove()
}
})
}
You have two different markers in your code:
result of this.map.addMarker();
result of this.map.addMarkerSync().
You have a nested structure and you lose one marker. Probably this one which you want to remove. Try to rename at least one of the variables to avoid confusion.

drawing a path between two marker on ionic 3

Hey im new on ionic i wanted to draw a path between two markers (two points) using the google maps api but im blocked with that piece of code
Well by seeing a lot of tutorial i can't find any documentation about the route drawing by ionic their github repository never mentionned the route drawing i think it's not supported .
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: this.agence.latitude,
//43.0741904,
lng: this.agence.longitude,
//-89.3809802
},
zoom: 14,
tilt: 30
}
};
this.map = GoogleMaps.create('map', mapOptions);
let marker: Marker = this.map.addMarkerSync({
title: 'Radeema'+this.agence.nom,
icon: 'red',
animation: 'DROP',
position: {
lat: this.agence.latitude,
lng: this.agence.longitude
}
});
let positionMarker: Marker = this.map.addMarkerSync({
title: 'votre position',
icon: 'blue',
animation: 'DROP',
position: {
lat: this.latitude,
lng: this.longitude,
}
});
this.map.addMarker({
title: 'votre position',
icon: 'green',
animation: 'DROP',
position: {
lat: this.latitude,
lng: this.longitude,
}
})
let directionsService = new google.maps.DirectionsService;
let directionsDisplay = new google.maps.DirectionsRenderer;
directionsService.route({
origin:{"lat": this.latitude, "lng": this.longitude} ,
destination: {"lat": this.agence.latitude, "lng": this.agence.longitude},
travelMode: google.maps.TravelMode['DRIVING']
}, (res, status) => {
if(status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(res);
} else {
console.warn(status);
}
});
this.map.addPolyline({
points: [
{"lat": this.latitude, "lng": this.longitude},
{"lat": this.agence.latitude, "lng": this.agence.longitude},
],
'color' : '#fff51e',
'width': 10,
'geodesic': true
}); }
what should i do in this case i can't understand the source of the error
From documentation, converted from js to ts:
private directionService = new google.maps.DirectionsService;
private directionsDisplay = new google.maps.DirectionsRenderer;
initMap() {
let map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: { lat: 41.85, lng: -87.65 }
});
this.directionsDisplay.setMap(map);
}
onChangeHandler() { // use this function in your dom or in your code when you want to dislpay the route
calculateAndDisplayRoute();
};
calculateAndDisplayRoute() {
this.directionsService.route({
origin: userLocation,
destination: agenceLocation,
travelMode: 'DRIVING'
}, (response, status) => {
if (status === 'OK') {
this.directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
However I recommand this plugin which let the user chose his favorite navigation application.

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 maps sencha touch 2 configuration

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
}
}
});

Google maps marker and Sencha touch 2

I have an application made in Sencha Touch 2 that uses a Google Map. The map is going to display the position of several markers loaded from a JSON file with an Store. My question is, how can I read the markers from my Store? how can I use the data from the store to generate the markers?
This is what I have and IT IS WORKING FIND because I have an inline array "neighborhoods":
The Map:
Ext.define('MyApp.view.detalleMapa', {
extend: 'Ext.Map',
alias: 'widget.detallemapa',
config: {
listeners: [
{
fn: 'onMapMaprender',
event: 'maprender'
}
]
},
onMapMaprender: function(map, gmap, options) {
var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];
for (var i = 0; i < neighborhoods.length; i++) {
var m = neighborhoods[i];
new google.maps.Marker({
position: m,
map: gmap,
draggable: false,
animation: google.maps.Animation.DROP
});
}
}
});
The Store:
Ext.define('MyApp.store.itemsStore', {
extend: 'Ext.data.Store',
alias: 'store.itemsstore',
requires: [
'MyApp.model.modelo'
],
config: {
autoLoad: true,
model: 'MyApp.model.modelo',
storeId: 'itemsStoreId',
proxy: {
type: 'ajax',
url: '/markersJsonFormat.json',
reader: {
type: 'json',
rootProperty: ''
}
}
}
});
So far everything works fine because I use the array-markers "neighborhoods" into the map-class, but how can I use the array that was loaded by the store from the file 'markersJsonFormat.json'?
any idea?
Thanks! :)
PD: this is my app:
Ext.application({
requires: [
'MyApp.view.override.detalleMapa'
],
models: [
'modelo'
],
stores: [
'itemsStore'
],
views: [
'principalTabPanel',
'navegacionView',
'itemsLista',
'detalleMapa'
],
name: 'MyApp',
launch: function() {
Ext.create('MyApp.view.detalleMapa', {fullscreen: true});
}
});
This is the answer in case someone is looking:
onMapMaprender: function(map, gmap, options) {
var store = Ext.getStore('itemsstore'); // Ext.getStore('StoreId')
var count = store.getCount();
//debugger;
store.load({
scope: this,
callback: function(records) {
//debugger;
this.processTweets(records);
}
});
},
Be careful because this will load the URL source again and will refresh the store content in case you already loaded.