Browse Source

Add ROUTE urls to show a map with start/destination (Gmaps, OSM and Here)

pull/134/head
Alexis de Lattre 9 years ago
parent
commit
0b71c3b566
  1. 14
      partner_address_on_map/map_url_data.xml
  2. 3
      partner_address_on_map/map_url_view.xml
  3. 103
      partner_address_on_map/partner_address_on_map.py
  4. 4
      partner_address_on_map/partner_view.xml
  5. 2
      partner_address_on_map/users_view.xml

14
partner_address_on_map/map_url_data.xml

@ -10,33 +10,37 @@
<record id="google_maps" model="map.url">
<field name="name">Google Maps</field>
<field name="address_url">https://www.google.com/maps?ie=UTF8&amp;q=</field>
<field name="address_url">https://www.google.com/maps?ie=UTF8&amp;q={ADDRESS}</field>
<field name="lat_lon_url">https://www.google.com/maps?z=15&amp;q={LATITUDE},{LONGITUDE}</field>
<field name="route_address_url">https://www.google.com/maps?saddr={START_ADDRESS}&amp;daddr={DEST_ADDRESS}&amp;directionsmode=driving</field>
<field name="route_lat_lon_url">https://www.google.com/maps?saddr={START_LATITUDE},{START_LONGITUDE}&amp;daddr={DEST_LATITUDE},{DEST_LONGITUDE}&amp;directionsmode=driving</field>
</record>
<record id="openstreetmap" model="map.url">
<field name="name">OpenStreetMap</field>
<field name="address_url">https://nominatim.openstreetmap.org/search?q=</field>
<field name="address_url">https://nominatim.openstreetmap.org/search?q={ADDRESS}</field>
<field name="lat_lon_url">https://www.openstreetmap.org/?zoom=15&amp;mlat={LATITUDE}&amp;mlon={LONGITUDE}</field>
<field name="route_lat_lon_url">https://www.openstreetmap.org/directions/?engine=orsm_car&amp;route={START_LATITUDE},{START_LONGITUDE};{DEST_LATITUDE},{DEST_LONGITUDE}</field>
</record>
<record id="openstreetmap_fr" model="map.url">
<field name="name">OpenStreetMap FR</field>
<field name="address_url">http://tile.openstreetmap.fr/?q=</field>
<field name="address_url">http://tile.openstreetmap.fr/?q={ADDRESS}</field>
<field name="lat_lon_url">http://tile.openstreetmap.fr/?zoom=15&amp;lat={LATITUDE}&amp;lon={LONGITUDE}</field>
</record>
<record id="bing_maps" model="map.url">
<field name="name">Bing Maps</field>
<field name="address_url">https://www.bing.com/maps/default.aspx?rtp=adr.</field>
<field name="address_url">https://www.bing.com/maps/default.aspx?where1={ADDRESS}</field>
<field name="lat_lon_url">https://www.bing.com/maps/default.aspx?cp={LATITUDE}~{LONGITUDE}&amp;lvl=15</field>
</record>
<record id="here" model="map.url">
<field name="name">Here Maps</field>
<field name="address_url">https://here.com/search/</field>
<field name="address_url">https://here.com/search/{ADDRESS}</field>
<field name="lat_lon_url">https://www.here.com/?map={LATITUDE},{LONGITUDE},15,normal</field>
<!-- TODO : for here maps, try to find a lat_lon_url with dot -->
<field name="route_lat_lon_url">https://www.here.com/directions/drive/:{START_LATITUDE},{START_LONGITUDE}/:{DEST_LATITUDE},{DEST_LONGITUDE}</field>
</record>
</data>

3
partner_address_on_map/map_url_view.xml

@ -17,6 +17,8 @@
<field name="name"/>
<field name="address_url"/>
<field name="lat_lon_url"/>
<field name="route_address_url"/>
<field name="route_lat_lon_url"/>
</group>
</form>
</field>
@ -29,7 +31,6 @@
<tree string="Map Providers">
<field name="name"/>
<field name="address_url"/>
<field name="lat_lon_url"/>
</tree>
</field>
</record>

103
partner_address_on_map/partner_address_on_map.py

@ -31,20 +31,32 @@ class MapUrl(models.Model):
name = fields.Char(string='Map Provider', required=True)
address_url = fields.Char(
string='URL that uses the address',
help="Odoo will add the address as string at the end of this URL.")
help="In this URL, {ADDRESS} will be replaced by the address.")
lat_lon_url = fields.Char(
string='URL that uses latitude and longitude',
help="In this URL, {LATITUDE} will be replaced by the latitude and "
"{LONGITUDE} will be replaced by the longitude (requires the module "
"base_geolocalize)")
help="In this URL, {LATITUDE} and {LONGITUDE} will be replaced by "
"the latitude and longitude (requires the module 'base_geolocalize')")
route_address_url = fields.Char(
string='Route URL that uses the addresses',
help="In this URL, {START_ADDRESS} and {DEST_ADDRESS} will be "
"replaced by the start and destination addresses.")
route_lat_lon_url = fields.Char(
string='Route URL that uses latitude and longitude',
help="In this URL, {START_LATITUDE}, {START_LONGITUDE}, "
"{DEST_LATITUDE} and {DEST_LONGITUDE} will be replaced by the "
"latitude and longitude of the start and destination adresses "
"(requires the module 'base_geolocalize').")
class ResUsers(models.Model):
_inherit = 'res.users'
# begin with context_ to allow user to change it by himself
context_map_url_id = fields.Many2one(
'map.url', string='Map Provider')
context_map_url_id = fields.Many2one('map.url', string='Map Provider')
# IDEA : should we add the ability to have 1 map provider for map
# and another one for routing ?
context_route_start_partner_id = fields.Many2one(
'res.partner', string='Start Address for Route Map')
# called from post-install script
# I can't use a default method on the field, because it would be executed
@ -59,6 +71,21 @@ class ResUsers(models.Model):
class ResPartner(models.Model):
_inherit = 'res.partner'
@api.model
def address_as_string(self):
addr = u''
if self.street:
addr += self.street
if self.street2:
addr += u' ' + self.street2
if self.city:
addr += u' ' + self.city
if self.state_id:
addr += u' ' + self.state_id.name
if self.country_id:
addr += u' ' + self.country_id.name
return addr
@api.multi
def open_map(self):
if not self.env.user.context_map_url_id:
@ -78,17 +105,59 @@ class ResPartner(models.Model):
raise Warning(
_("Missing parameter 'URL that uses the address' "
"for map provider '%s'.") % map_url.name)
url = map_url.address_url
if self.street:
url += self.street
if self.street2:
url += ' ' + self.street2
if self.city:
url += ' ' + self.city
if self.state_id:
url += ' ' + self.state_id.name
if self.country_id:
url += ' ' + self.country_id.name
url = map_url.address_url.replace(
'{ADDRESS}', self.address_as_string())
return {
'type': 'ir.actions.act_url',
'url': url,
'target': 'new',
}
@api.multi
def open_route_map(self):
if not self.env.user.context_map_url_id:
raise Warning(
_('Missing map provider: '
'you should set it in your preferences.'))
map_url = self.env.user.context_map_url_id
if not self.env.user.context_route_start_partner_id:
raise Warning(
_('Missing start address for route map: '
'you should set it in your preferences.'))
start_partner = self.env.user.context_route_start_partner_id
if map_url.route_address_url:
start_address = start_partner.address_as_string()
dest_address = self.address_as_string()
url = map_url.route_address_url
url = url.replace('{START_ADDRESS}', start_address)
url = url.replace('{DEST_ADDRESS}', dest_address)
else:
if not hasattr(self, 'partner_latitude'):
raise Warning(
_("Missing module 'base_geolocalize'"))
if not map_url.route_lat_lon_url:
raise Warning(
_("No route URL that uses latitude and longitude "
"on map provider '%s'.") % map_url.name)
url = map_url.route_lat_lon_url
if (
not start_partner.partner_latitude or
not start_partner.partner_longitude):
raise Warning(
_("Missing geo-localization information on "
"start partner '%s'."))
if not self.partner_latitude or not self.partner_longitude:
raise Warning(
_("Missing geo-localization information on destination "
"partner '%s'.") % self.name)
url = url.replace(
'{START_LATITUDE}', unicode(start_partner.partner_latitude))
url = url.replace(
'{START_LONGITUDE}', unicode(start_partner.partner_longitude))
url = url.replace(
'{DEST_LATITUDE}', unicode(self.partner_latitude))
url = url.replace(
'{DEST_LONGITUDE}', unicode(self.partner_longitude))
return {
'type': 'ir.actions.act_url',
'url': url,

4
partner_address_on_map/partner_view.xml

@ -19,6 +19,10 @@
name="open_map" type="object" string="Map"
attrs="{'invisible': [('street', '=', False), ('city', '=', False)]}"
icon="fa-map-marker"/>
<button class="oe_inline oe_stat_button"
name="open_route_map" type="object" string="Route Map"
attrs="{'invisible': [('street', '=', False), ('city', '=', False)]}"
icon="fa-car"/>
</xpath>
</field>
</record>

2
partner_address_on_map/users_view.xml

@ -15,6 +15,7 @@
<field name="arch" type="xml">
<group name="preferences" position="inside">
<field name="context_map_url_id" widget="selection"/>
<field name="context_route_start_partner_id"/>
</group>
</field>
</record>
@ -26,6 +27,7 @@
<field name="arch" type="xml">
<group name="preferences" position="inside">
<field name="context_map_url_id" readonly="0" widget="selection"/>
<field name="context_route_start_partner_id" readonly="0"/>
</group>
</field>
</record>

Loading…
Cancel
Save