Forum Archive

location.render_map_snapshot() show_poinjas no effect

Acidham

location.render_map_snapshot(lat, lng, width=1000, height=1000, map_type='standard', show_poi=True, img_width=240, img_height=240, img_scale=0)

show_poi=True does not show poi on map. What is missing?

cvp

@Acidham try with hybrid instead of standard

img = location.render_map_snapshot(48.8582, 2.2945, map_type='hybrid', img_width=512, img_height=512) 

Acidham

Works! Is there a way to show pin at location/ center?

cvp

@Acidham I never use this location module and I don't think it offers the function you ask.
But you can use Apple mkmapview, there are some samples in the forum

cvp

@Acidham for instance here

omz

@Acidham It's relatively easy to draw the image of a pin (or anything else) on top of the result, but there's no way to place one directly with the location module. Here's an example of how to draw a pin on top:

import location
import ui

img = location.render_map_snapshot(48.8582, 2.2945, map_type='hybrid', img_width=512, img_height=512)
with ui.ImageContext(img.size.w, img.size.h) as ctx:
    img.draw()
    ui.Image('emj:Triangular_Flag').draw(256, 256)
    result = ctx.get_image()
    result.show()
cvp

@Acidham Try this

import location
from   math import radians, sin, cos, asin, sqrt, atan2, degrees, pi, pow
import ui

def calculate_distance(pointA, pointB):
    """
    :Parameters:
      - `pointA: The tuple representing the latitude/longitude for the
        first point. Latitude and longitude must be in decimal degrees
      - `pointB: The tuple representing the latitude/longitude for the
        second point. Latitude and longitude must be in decimal degrees

    :Returns:
      The distance in km 
    """
    # convert decimal degrees to radians 
    #print(pointA, pointB)
    lat1 = radians(pointA[0])
    lat2 = radians(pointB[0])
    lon1 = radians(pointA[1])
    lon2 = radians(pointB[1])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    dist = c * r
    #print(dist)
    return dist

lat_cen = 48.8582   # Tour Eiffel
lon_cen =  2.2945
w_m = 1000
h_m = 1000
img = location.render_map_snapshot(lat_cen, lon_cen, width=w_m, height=h_m, map_type='standard', show_poi=True, img_width=512, img_height=512, img_scale=0)
lat_pin = 48.8560793    # Bassins du Champ de Mars
lon_pin =  2.2979048
x = calculate_distance((lat_cen,lon_cen),(lat_cen,lon_pin))
y = calculate_distance((lat_cen,lon_cen),(lat_pin,lon_cen))
#print(x,y)
x = img.size.w/2 + x*img.size.w/(w_m/1000)
y = img.size.h/2 + y*img.size.h/(h_m/1000)
with ui.ImageContext(img.size.w, img.size.h) as ctx:
    img.draw()
    d = 8
    path = ui.Path.oval(x-d,y-d,2*d,2*d)
    ui.set_color('red')
    path.move_to(x-d,y)
    path.line_to(x+d,y)
    path.move_to(x,y-d)
    path.line_to(x,y+d)
    path.stroke()

    result = ctx.get_image()
    result.show()