在setMyLocationEnabled eclipse上的谷歌地图上定义我的位置

时间:2016-06-11 22:58:05

标签: java android eclipse google-maps

我正在开发一款带谷歌地图的Android应用程序。问题是,当我在谷歌地图上激活“setMyLocationEnabled”时,它将我的位置设置为香港,但我在葡萄牙。但是locationManager给出的位置给了我正确的位置。有没有办法定义我的位置是“setMyLocationEnabled”上locationManager的位置?

这给了我正确的位置:

from tkinter import *
from tkinter import ttk
from datetime import *
import locale
# for currency display, set the locale
locale.setlocale( locale.LC_ALL, '' )



class main_gui(Frame):

    def __init__(self):                                                 #Intialize key variables and call the main screen.
        self.Ledgerdata = [ ]
        print ("main_gui __init__")
        self.load_data()
        self.main_screen()

    def load_data(self):
        # stub for testing - to be rewritten to read external file and load into Ledgerdata.

        entry = []
        entry.append("4/17/2016, 24, 1, Doctor")
        entry.append("4/18/2016, 32, 1, Mechanic whose name is bill")
        entry.append("4/19/2016, 45, 1, Grocery")
        entry.append("4/11/2016, 19, 1, Puppet")
        entry.append("4/17/2016, 119.50, 1, Johns Computer House")
        entry.append("4/11/2016, 1250, 1, Sidney Barthalmew")

        for entry_index in range(len(entry)):
            entry[entry_index] = entry[entry_index].split(",")

            date_conversion = entry[entry_index][0].split("/")  # Convert date from text string
            entry[entry_index][0] = date(int(date_conversion[2]),  # to datetime variable
                                         int(date_conversion[0]),
                                         int(date_conversion[1]))
            self.Ledgerdata.append((entry[entry_index][0],  # Add transaction to Ledgerdata
                                                        float(entry[entry_index][1]),
                                                        int(entry[entry_index][2]),
                                                        entry[entry_index][3]))
#====== Data from file is read and loaded into Ledgerdata

    #main_screen - main display screen for Ledgerdata
    #  Will show list of transactions, total for each category,
    #  with buttons to Add, Edit, Delete, Create Summary and Exit
    def main_screen(self):
        root = Tk()                                                     # reference to the GUI toolkit
        main_window = ttk.Frame(root)
        main_window.grid(row=0,column=0)                                # overall Frame
        display_frame = ttk.Frame(main_window)                          # list display frame
        display_frame.grid(column=0, row = 0)
        option_frame = ttk.Frame(main_window)                       # selection frame - to be written.
        option_frame.grid(column=0, row=15, columnspan=8)
        summary_frame = ttk.Frame(main_window)
        summary_frame.grid(column = 10, row = 0)
        summary_frame.columnconfigure(0, minsize=50)
        summary_frame.columnconfigure(1, minsize=250)

        #Start display frame

        list_header = Label(display_frame, width = 100,                  # Header line for List
                            font = 12,anchor = W,                       #    NEEDS CLEANUP
                            text = "   Date       Amount    Description                Code")
        list_header.grid(row=1)

        list_of_transactions = Listbox(display_frame,width =100, height = 20, font = 12)  #Generate listbox widget
        list_of_transactions.grid(row=2, column = 0)

        # Loop for number of transactions in Ledgerdata.  Loop adds formatted transaction info to list_of_transactions
        #   liststring object for display
        for x in range(len(self.Ledgerdata)):
            output_string = '{0:10}  {1:>8}  {2:<50} {3:<40}'.format(
                                                     str(self.Ledgerdata[x][0].strftime("%m/%d/%y"))
                                                     ,locale.currency(self.Ledgerdata[x][1], grouping=True)
                                                     ,(self.Ledgerdata[x][3])
                                                     ,(self.Ledgerdata[x][2]))
            list_of_transactions.insert(x,output_string)
            list_of_transactions.grid(row=x, column = 0)




        #main_window.pack()
        mainloop()


main_gui()

但是这个:

Criteria criteria = new Criteria();
        Location myLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

        Lat = myLocation.getLatitude();
        Long = myLocation.getLongitude();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 1, this);

我说我在葡萄牙的时候就在香港。

如何定义“setMyLocationEnabled”必须从与locationManager相同的位置开始并将蓝点放在该位置?

1 个答案:

答案 0 :(得分:0)

在使用My Location layer时,请注明

  

在启用“我的位置”图层之前,必须确保您具有所需的运行时位置权限。

粗略的位置许可:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  ...
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  ...
</manifest>

或精美的位置许可:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  ...
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  ...
</manifest>

但是,如Making Your App Location-Aware

中所述
  

Google Play services location APIs优于Android框架位置API(android.location),作为向您的应用添加位置感知的一种方式。如果您当前正在使用Android框架位置API,强烈建议您尽快切换到Google Play服务位置API。