sencha touch的当前位置



我使用以下代码在Sencha Touch 2上显示我的当前位置。它在console.log()中显示了正确的纬度,但没有显示地图。请帮忙。

Ext.define('restApp.view.GreetingView', {
extend: 'Ext.Map',
alias: 'widget.mymap',

config: {
fullscreen: true,
//tpl: '<p>The ID is {uuid}</p><p>The content is {display}</p>',
layout: {
type: 'fit'
}
},
initialize:function(){
Ext.Viewport.add({
xtype: 'map',
id:'geomap',
itemId:'ma'
});
var geo = Ext.create('Ext.util.Geolocation', {
autoUpdate: true,
frequency: '10000',
listeners: {
locationupdate: function (geo) {        
var center = new google.maps.LatLng(geo.getLatitude(), geo.getLongitude());
Ext.getCmp('geomap').setData(center);
//restApp.view.GreetingView.getComponent('ma').update(center);
var marker = new google.maps.Marker({
position: center,
map: Ext.getCmp('geomap').map
});
console.log('New latitude: '+ geo.getLatitude());
},
locationerror: function (geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
if (bTimeout) {
alert('Timeout occurred.');
} 
else {
alert('Error occurred.');
}
}
}
});
}
});

旧答案

将它与我用于相同目的的片段(如下所示)进行比较,我意识到问题很简单。"中心"是一个保留词。请尝试使用其他变量名。

编辑部分:删除代码片段。

新答案

我环顾四周,发现你的"项目"只是一个零散的演示代码集合。

这里有一个完整的代码解决方案,为了简单起见,去掉了所有多余的部分,以及过度使用变量,也扩展为冗长的格式。

/*
The application, including a simple launcher.
*/
Ext.application({
requires    : ['Ext.device.Geolocation'],
views       : ['MainView'],
controllers : ['Maps'],
name        : 'Geo',
launch: function() {
Ext.create('Geo.view.MainView', {fullscreen: true});
}
});
/*
The View to display the map, as well as how to include the navigation bar
and include a "you are here" button.
*/
Ext.define('Geo.view.MainView', {
extend: 'Ext.navigation.View',
alias: 'widget.mainview',
requires: [
'Ext.Panel',
'Ext.Map',
'Ext.navigation.Bar',
'Ext.Button'
],
config: {
items: [
{
xtype  : 'panel',
title  : 'Map',
itemId : 'mapPanel',
items  : [
{
xtype: 'map',
height: '100%',
itemId: 'map'
}
]
}
],
navigationBar: {
docked: 'top',
items: [
{
xtype: 'button',
itemId: 'youAreHereButton',
text: 'You Are Here'
}
]
}
}
});

/* 
The Controller with functionality for the "you are here" button tap 
*/
Ext.define('Geo.controller.Maps', {
extend: 'Ext.app.Controller',
config: {
refs: {
mapView: {
selector: 'mainview #map',
xtype: 'Ext.Map'
},
mainView: {
selector: 'mainview',
xtype: 'Ext.navigation.View'
}
},
control: {
"mainview #youAreHereButton": {
tap: 'onYouAreHereTap'
}
}
},
onYouAreHereTap: function(button, e, eOpts) {
// set 'mapView' as the parent view displaying the map
var mapView = this.getMapView();
// control measure for old browsers or denied permission for location detection.
if (Ext.feature.has.Geolocation) {
/*
Ext.device.Geolocation uses native (phone) Geolocation capabilities if available,
and falls back to Ext.util.Geolocation if only browser support detected.
*/
Ext.device.Geolocation.getCurrentPosition({
allowHighAccuracy : true,
maximumAge        : 0,
timeout           : 20000,
success           : function(position) {
var latitude  = position.coords.latitude,
longitude = position.coords.longitude,
location  = new google.maps.LatLng(latitude, longitude),
marker    = new google.maps.Marker({
position  : location,
map       : mapView.getMap(),
animation : google.maps.Animation.DROP
});
mapView.setMapOptions({   // Move to the center
center: location
});
},
failure: function() {
console.log('something went wrong!');
}
});
}
}
});

是的,我本可以将其进一步简化为一个单独的视图,其中还包含"您在这里"点击的控制器处理程序。我选择以这种方式来展示它,以帮助您理解MVC模式及其在Sencha Touch中的应用。

为了实现这一点,它需要Sencha Touch库,以及以下行:

<script src="http://maps.google.com/maps/api/js?sensor=true"></script>

这是一个脚本,它将谷歌地图包含在您的页面中,对显示至关重要。

了解更多:

https://www.sencha.com/learn/hello-world/-开始

http://docs.sencha.com/touch/2.3.1/#/指南-关于如何在Sencha Touch中做任何事情的完整文档,从指南页面开始。

最新更新