2013-03-12 50 views
2

我戳在與谷歌地圖API的PyMaps,這裏是示例代碼,我從 http://www.lonelycode.com/2008/12/04/google-maps-and-django/PyMaps變量沒有發現

def showmap(): 
from pymaps import Map, PyMap # import the libraries 

# Create a map - pymaps allows multiple maps in an object 
tmap = Map() 
tmap.zoom = 3 

# Latitude and lognitude - see the getcords function 
# to see how we convert from traditional D/M/S to the DD type 
# used by Googel Maps 

lat = 0.0 
long = 0.0 

# These coordinates are for Hong Kong 
dlat = "22 15 0 N" 
dlong = "114 10 60 E" 

dlat = dcode.lat.split(" ") 
dlong = dcode.long.split(" ") 

# Convert the coordinates 
lat = getcords(float(dlat[0]), float(dlat[1]), float(dlat[2]), dlat[3]) 
long = getcords(float(dlong[0]), float(dlong[1]), float(dlong[2]), dlong[3]) 

# Inserts html into the hover effect 
pointhtml = "Hello!" 

# Add the point to the map 
point = (lat, long, pointhtml, icon.id) 

tmap.setpoint(point) 
tmap.center = (1.757537,144.492188) 

# Put your own googl ekey here 
gmap = PyMap(key=GOOGLE_KEY, maplist=[tmap]) 
gmap.addicon(icon) 

# pymapjs exports all the javascript required to build the map! 
mapcode = gmap.pymapjs() 

# Do what you want with it - pass it to the template or print it! 
return mapcode 

發現問題是,我得到當我錯誤運行程序,我得到「NameError:全局名稱'dcode'未定義」dcode究竟是什麼,我在哪裏可以找到它?這是錯誤的嗎?

回答

0

我的猜測是,它應該是這樣的:

# These coordinates are for Hong Kong 
dlat = "22 15 0 N" 
dlong = "114 10 60 E" 

dlat = dlat.split(" ") 
dlong = dlong.split(" ") 

如果你還沒有導入getcords功能隨時隨地任你可能會在這裏遇到麻煩。

# Convert the coordinates 
lat = getcords(float(dlat[0]), float(dlat[1]), float(dlat[2]), dlat[3]) 
long = getcords(float(dlong[0]), float(dlong[1]), float(dlong[2]), dlong[3]) 

而且我建議你不要使用long作爲變量名,因爲它是一個int類型。

In [98]: long? 
Type:  type 
String Form:<type 'long'> 
Namespace: Python builtin 
+0

是的,我清理了一下代碼,long是一個可怕的變量名。我只是貼了原件。謝謝! – 2013-03-12 20:02:21