【问题标题】:Markers not showing on Google Map in Google App Engine标记未显示在 Google App Engine 中的 Google 地图上
【发布时间】:2011-08-07 19:17:54
【问题描述】:

我在 Python 中创建了一个 Google App Engine 项目,它在我的本地主机上运行,​​但是当我将它上传到 geo-event-maps.appspot.com 时,标记没有显示。 我有一个运行来调用 /place 的 cron。 我没有日志错误 我的数据存储是空的! txt 文件正在上传:

    file_path = os.path.dirname(__file__)
    path = os.path.join(file_path, 'storing', 'txtFiles')

有没有办法检查文件是否已上传?!

我完全不知所措。以前有人遇到过这些问题吗?

下面是我的main.py:

    '''
Created on Mar 30, 2011

@author: kimmasterson
'''
#!/usr/bin/env python

from google.appengine.ext import webapp
from google.appengine.ext import db
from placemaker import placemaker
import logging
import  wsgiref.handlers
import os, glob
from google.appengine.dist import use_library
use_library('django', '1.2')
from google.appengine.ext.webapp import template


class Story(db.Model):
    id = db.StringProperty()
    loc_name = db.StringProperty()
    title = db.StringProperty()
    long = db.FloatProperty()
    lat = db.FloatProperty()
    link = db.StringProperty()
    date = db.StringProperty()
class MyStories(webapp.RequestHandler):
    def get(self):
        temp = db.Query(Story)
        temp = temp.count()


        story_set = Story.all()

        template_values = {
            'storyTemp': story_set
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))
class place(webapp.RequestHandler):
    def get(self):
        #path = '/storing/txtFiles'
        file_path = os.path.dirname(__file__)
        path = os.path.join(file_path, 'storing', 'txtFiles')

        try:
            for infile in glob.glob(os.path.join(path, '*.txt')):
                    #print infile
                    f = open(infile, 'r')
                    data = f.read()
                    newfile = infile.replace('.txt', '')
                    newfile = newfile.replace('/storing/txtFiles/', '')
                    #print newfile
                    storyname = 'http://www.independent.ie/national-news/' + newfile
                    #print storyname
                    #print newfile
                    #logging.info(data)
                    p = placemaker('HSnG9pPV34EUBcexz.tDYuSrZ8Hnp.LowswI7TxreF8sXrdpVyVIKB4uPGXBYOA9VjjF1Ca42ipd_KhdJsKYjI5cXRo0eJM-')
                    print p.find_places(data)
                    for place in p.places:
                        splitted = place.name.split()
                        for word in splitted:
                            temp = db.Query(Story)
                            temp = temp.filter("link = ", storyname)
                            results = temp.fetch(limit=1)
                            if len(results) > 0:
                                break
                            elif 'IE' in word:
                                print temp
                                print 'success'
                                print 'name of the file is:' + newfile
                                story = Story(name=newfile, long=place.centroid.longitude, lat=place.centroid.latitude, link=storyname, loc_name=place.name, title=newfile).put()
                                #logging.info(type(place.centroid.latitude))    
        except:
            print 'error'

def main():
    application = webapp.WSGIApplication([('/', MyStories), ('/place', place)],
                                         debug=True)

    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()

这是我的 cron.yaml

    cron:
- description: running place
  url: /place
  schedule: every day 11:05

App.yaml 如下:

application: geo-event-maps
version: 2
runtime: python
api_version: 1

handlers:
- url: .*
  script: main.py
builtins:
- datastore_admin: on

【问题讨论】:

  • 这是否在本地使用 dev_appserver 工作?你确定你的infile in glob.glob(... 块正在运行吗?也许您的文件不在您期望它们在生产中的位置。我认为您应该稍微缩小问题范围并更新您的帖子。
  • 这就是我想知道如何确定我的文件是否被添加到服务器上的原因。它适用于本地主机,地图在我的 apppot 上运行,但由于数据存储为空,所以没有标记。

标签: python google-app-engine google-maps cron google-maps-markers


【解决方案1】:

您需要确保您的文件与您的应用程序代码一起上传,它们不能被标记为静态文件,否​​则您的代码将无法访问它们。使用 --verbose 标志运行 appcfg.py 并确保它们已上传。

第二个问题,在您的place 类中,您将路径定义为path = '/storing/txtFiles'。那是错的。您的路径可能更像是:

file_path = os.path.dirname(__file__)
path = os.path.join(file_path, 'storing', 'txtFiles')

另外,我建议你不要使用print,而是使用self.response.out.write(stuff_to_write)

您可能还想了解如何使用 key_names。通过在嵌套的 for 循环中运行批处理 db.get 而不是 db.Query ,您将能够使您的代码更加高效。使用Appstats 并尽量减少RPC 的数量。

【讨论】:

  • 您可能应该使用logging.debug(stuff_to_write) 而不是在响应中返回通过 cron/taskqueue 调用时不可见的内容。
  • @Chris 是的,使用 logging.debug() 可能会更好。所有这些打印调用看起来像是在直接调用 url 进行测试时使用的临时调试辅助工具。
  • 我更改了 main.py 以读取每个 HTML 文件并将我需要的位添加到数据存储中,并且只是查询数据存储而不是尝试加载文件。非常感谢您的帮助:-)
【解决方案2】:

首先确保您使用相对路径访问您的文件。

接下来确保您没有在 app.yaml 中将文件标记为静态文件,因为静态文件不会上传到与您的应用程序相同的位置(它们被发送到 Google 前端服务器可以更直接地为它们提供服务的地方)。

【讨论】:

  • 当我部署应用程序时,它显示“正在克隆 919 个应用程序文件。克隆了 100 个文件。克隆了 200 个文件。克隆了 300 个文件。克隆了 400 个文件。克隆了 500 个文件。克隆了 600 个文件。克隆了 700 个文件。克隆了 800 个文件。克隆了 900 个文件。“这是否意味着文件肯定会上传到服务器?
  • 重点不在于它们是否被上传(他们是),而在于它们是否被上传到 GAE 静态文件服务器或 GAE 应用程序服务器。您的 python 代码只能访问发送到您的应用程序服务器的内容,因此如果您的 txt 文件位于 app.yaml 中标记为静态的目录中,您将无法 open() 它。
猜你喜欢
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2020-04-10
  • 2021-07-12
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多