GithubHelp home page GithubHelp logo

gustotc / kivy-gmaps Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tito/kivy-gmaps

0.0 2.0 0.0 1.85 MB

Google maps integration within Kivy application

License: MIT License

Python 95.56% Java 4.44%

kivy-gmaps's Introduction

Kivy / Google Maps integration

Example that shows how to integrate Google Maps within a Kivy application. Currently works only on Android.

Work in progress

ScreenShot

Configuration

  • Configure your package.name and package.domain. The sum of both should not be more than 20 characters, or Google Maps API will not work:

      package.name = kivygmaps
      package.domain = com.mr
    
  • Follow the Android Maps API v2 documentation to get your Android Maps API key

  • Put your API Key into buildozer.spec in [app:android.meta_data] section as com.google.android.maps.v2.API_KEY:

      [app:android.meta_data]
      com.google.android.maps.v2.API_KEY = YOURAPIKEYHERE
    

Usage

The gmaps module provide a GMap widget that you can directly use into your application. Right now, you can add only one GMap per application, on an transparent background. If you cover the background, the widget will not be shown. Here is an hello world example:

import gmaps
from kivy.app import App

class HelloGmaps(App):
    def build(self):
        self.map_widget = GMap()
        self.map_widget.bind(on_ready=self.create_some_markers)
        return self.map_widget

    def create_some_markers(self, map_widget):
        # get the google map interface
        sydney = map_widget.create_latlng(-33.867, 151.206)
        marker = map_widget.create_marker(
            title='Sydney',
            snippet='The most populous city in Autralia',
            position=sydney)
        map_widget.map.addMarker(marker)

if __name__ == '__main__':
    HelloGmaps().run()

Threads

All the operation done on the map must be done within the internal thread of the android widget. It means, you cannot interact directly from Kivy. You can create object (LatLng, Marker..), but interact with the map must stay in the map thread.

All the event fired by the GMap are happening in the map thread. For example, if you bind to the GMap.on_map_click, your callback will be already in the map thread. If you want to call a method within the map thread, you can use GMap.execute() method. You cannot return values from it, and the call will be asynchronous:

map = GMap()
def my_func(...):
    pass
map.execute(my_func)

You can also decorate your function with android.runnable.run_on_ui_thread, or GMap.run_on_ui_thread:

from android.runnable import run_on_ui_thread

@run_on_ui_thread
def set_position(self, lat, lng):
    pos = self.map_widget.create_latlng(lat, lng)
    self.map_widget.map.moveCamera(
        self.map_widget.camera_update_factory.newLatLngZoom(
        pos, 13))

When the Android Google Maps widget is created, it will fire an event name on_ready. This is where you can create markers, change the camera, and so on.

Events

Supported Google Maps events are:

  • on_camera_change(CameraPosition) (not working yet)
  • on_info_window_click(Marker)
  • on_map_click(LatLng)
  • on_map_loaded() (not working yet)
  • on_map_long_click(Marker)
  • on_marker_click(Marker)
  • on_marker_drag(Marker)
  • on_marker_drag_end(Marker)
  • on_marker_drag_start(Marker)
  • on_my_location_button_click

All the events are dispatched in the map thread, not the kivy main thread.

Utils

The GMap have some methods to create java object easily, such as:

  • GMap.create_latlng(lat, lng) - Create a LatLng object
  • GMap.create_marker(**options) Create a Marker object, with all the key=value in options

Authors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.