NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Please let me know how can i use open street map in my asp.net c# web application .. can any sample demo available or not ..

i don't use open street map so i don't have any idea about how can i use ..

asked 18 Jan '14, 07:20

shivanic77's gravatar image

shivanic77
15111
accept rate: 0%

1

So what do you want to realise? Just displaying a map (online or offline)? Or also POI search, routing, ...? A first starting point is http://wiki.openstreetmap.org/wiki/Frameworks

(18 Jan '14, 09:14) iii

Hi, I made it and it works.

link to download File01 and File 02: http://listefacil.com.br/busca_resultados.rar

File 01 (Busca_resultados.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/ListeMasterPage.master" AutoEventWireup="true" CodeFile="Busca_resultados.aspx.cs" Inherits="Busca_resultados" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>

    <style>
        html, body {
            height: 100%;
            margin: 0;
        }
        #map {
            width: 600px;
            height: 400px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <br />
    <br />
    <br />
    <br />
    <div id="map" style="width: 100%; height: 350px;"></div>
                        <script>

                            var markers = L.layerGroup();
                            <asp:Repeater ID="rptMarkers" runat="server">
                                <ItemTemplate>
                                        L.marker([<%# Eval("emp_lat") %>, <%# Eval("emp_lng") %>]).bindPopup('<h6><b><%# Eval("emp_nome") %></b></h6>Fone:<%# Eval("emp_fone") %><br><br><a href=empresa.aspx?emp_id=<%# Eval("emp_id") %>&p=<%# Eval("emp_plano") %>>VEJA MAIS DETALHES...</a>').addTo(markers);
                                </ItemTemplate>
                            </asp:Repeater>


                            var mbAttr = 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
                                    '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                                    'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
                                mbUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';

                            var grayscale   = L.tileLayer(mbUrl, {id: 'mapbox.light', attribution: mbAttr}),
                                streets  = L.tileLayer(mbUrl, {id: 'mapbox.streets',   attribution: mbAttr});

                            var map = L.map('map', {
                                center: [<%=var_lat_ini%>, <%=var_lng_ini%>],
                                zoom: 10,
                                layers: [grayscale, markers]
                            });

                            var baseLayers = {
                                "Grayscale": grayscale,
                                "Streets": streets
                            };

                            var overlays = {
                                "Markers": markers
                            };

                            L.control.layers(baseLayers, overlays).addTo(map);


                        </script>
</asp:Content>

Code Behind (Busca_resultados.aspx.cs) File 02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Npgsql;
using System.Data;
using System.Configuration;

public partial class Busca_resultados : System.Web.UI.Page
{


    public string getConstr()
    {
        string conStr = ConfigurationManager.ConnectionStrings["conPostCons"].ConnectionString;
        return conStr;
    }


    public string var_lat_ini;
    public string var_lng_ini;

    protected void Page_Load(object sender, EventArgs e)
    {

        string var_txt = Request.QueryString["txt"];

        if (var_txt == "")
        {


            NpgsqlConnection con = new NpgsqlConnection(getConstr());
            NpgsqlCommand cmd = new NpgsqlCommand("select tb_empresas.emp_id, tb_empresas.emp_nome, " +
                "tb_empresas.emp_plano, tb_empresas.emp_lat, tb_empresas.emp_lng, " +
                "tb_empresas.emp_logo, tb_empresas.emp_fone " +
                "from tb_empresas " +
                "where tb_empresas.uf_id=@uf_id and " +
                "tb_empresas.cid_id=@cid_id and " +
                "tb_empresas.sub_cat_id=@sub_cat_id and " +
                "tb_empresas.emp_plano <> 'g' and " +
                "tb_empresas.emp_data_fim >= @data_fim", con);

            try
            {
                cmd.Parameters.AddWithValue("@uf_id", Request.QueryString["uf_id"]);
                cmd.Parameters.AddWithValue("@cid_id", Request.QueryString["cid_id"]);
                cmd.Parameters.AddWithValue("@sub_cat_id", Request.QueryString["sub_cat_id"]);
                DateTime data_fim = DateTime.Now;
                cmd.Parameters.AddWithValue("@data_fim", data_fim);

                con.Open();
                NpgsqlDataReader dr;
                dr = cmd.ExecuteReader();


                if (dr.HasRows)
                {
                    DataTable dt = new DataTable("Tabela");

                    dt.Columns.Add("emp_id", typeof(string));
                    dt.Columns.Add("emp_nome", typeof(string));
                    dt.Columns.Add("emp_plano", typeof(string));
                    dt.Columns.Add("emp_lat", typeof(string));
                    dt.Columns.Add("emp_lng", typeof(string));
                    dt.Columns.Add("emp_fone", typeof(string));
                    dt.Columns.Add("emp_logo", typeof(string));

                    while (dr.Read())
                    {
                        dt.Rows.Add(dr["emp_id"].ToString(), dr["emp_nome"].ToString(), dr["emp_plano"].ToString(), dr["emp_lat"].ToString(), dr["emp_lng"].ToString(), dr["emp_fone"].ToString(), dr["emp_logo"].ToString());

                        // INICIO USADO EM O OPENSTREETMAP
                        var_lat_ini = dt.Rows[0]["emp_lat"].ToString();
                        var_lng_ini = dt.Rows[0]["emp_lng"].ToString();
                        // FIM USADO COM O OPENSTREETMAP
                    }

                    rptMarkers.DataSource = dt;
                    rptMarkers.DataBind();
                }


            }
            finally
            {
                con.Close();
            }
        }
        else
        {
            // O métdo Trim retira todos os espaços em branco a esquerda e a direita de nossa busca
            string var_busca_txt = Request.QueryString["txt"].Trim();
            string uf_id = Request.QueryString["uf_id"];
            string cid_id = Request.QueryString["cid_id"];

            string[] separar = var_busca_txt.Split(new Char[] { ' ' });

            //organiza a busca no campo emp_info da tb_empresas
            string palavra_info = "";
            foreach (string p in separar)
            {
                if (p.Trim() != "")
                {
                    palavra_info = palavra_info + "'%" + p + "%' or upper(emp_info) like ";
                }

            }
            //tratamento da string
            int tamanho_string_info = palavra_info.Length - 25;
            palavra_info = palavra_info.Substring(0, tamanho_string_info);



            //organiza a busca no campo emp_nome da tb_empresas
            string palavra_empresa = "";
            foreach (string p in separar)
            {
                if (p.Trim() != "")
                {
                    palavra_empresa = palavra_empresa + "'%" + p + "%' or upper(emp_nome) like ";
                }

            }
            //tratamento da string
            int tamanho_string_emp_nome = palavra_empresa.Length - 25;
            palavra_empresa = palavra_empresa.Substring(0, tamanho_string_emp_nome);

            NpgsqlConnection con = new NpgsqlConnection(getConstr());
            NpgsqlCommand cmd = new NpgsqlCommand("select tb_empresas.emp_id, tb_empresas.emp_nome, " +
                "tb_empresas.emp_plano, tb_empresas.emp_lat, tb_empresas.emp_lng, " +
                "tb_empresas.emp_logo, tb_empresas.emp_fone " +
                "from tb_empresas " +
                "where (upper(emp_info) like " + palavra_info.ToUpper() +
                " or (upper(emp_nome) like " + palavra_empresa.ToUpper() +
                ")) and tb_empresas.uf_id=@uf_id and " +
                "tb_empresas.cid_id=@cid_id and " +
                "tb_empresas.emp_plano <> 'g' and " +
                "tb_empresas.emp_data_fim >= @data_fim", con);

            try
            {
                cmd.Parameters.AddWithValue("@uf_id", uf_id);
                cmd.Parameters.AddWithValue("@cid_id", cid_id);
                DateTime data_fim = DateTime.Now;
                cmd.Parameters.AddWithValue("@data_fim", data_fim);
                con.Open();
                NpgsqlDataReader dr;
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    DataTable dt = new DataTable("Tabela");

                    dt.Columns.Add("emp_id", typeof(string));
                    dt.Columns.Add("emp_nome", typeof(string));
                    dt.Columns.Add("emp_plano", typeof(string));
                    dt.Columns.Add("emp_lat", typeof(string));
                    dt.Columns.Add("emp_lng", typeof(string));
                    dt.Columns.Add("emp_fone", typeof(string));
                    dt.Columns.Add("emp_logo", typeof(string));

                    while (dr.Read())
                    {
                        dt.Rows.Add(dr["emp_id"].ToString(), dr["emp_nome"].ToString(), dr["emp_plano"].ToString(), dr["emp_lat"].ToString(), dr["emp_lng"].ToString(), dr["emp_fone"].ToString(), dr["emp_logo"].ToString());

                        // INICIO USADO EM O OPENSTREETMAP
                        var_lat_ini = dt.Rows[0]["emp_lat"].ToString();
                        var_lng_ini = dt.Rows[0]["emp_lng"].ToString();
                        // FIM USADO COM O OPENSTREETMAP
                    }

                    rptMarkers.DataSource = dt;
                    rptMarkers.DataBind();
                }
            }
            finally
            {
                con.Close();
            }
        }

    }
}
permanent link

answered 21 Nov '18, 01:25

fbianof's gravatar image

fbianof
111
accept rate: 0%

edited 21 Nov '18, 07:46

aseerel4c26's gravatar image

aseerel4c26 ♦
32.6k18248554

Hi aseerel4c26

Do you have the full code?

(21 Oct '19, 23:25) vicula

@vicula: You probably meant to ask @fbianof.

(22 Oct '19, 09:14) TZorn

I think your best bet is the "BruTile" library - check out http://brutile.codeplex.com/. There's a couple of projects linked from there that use the library so you could use them as your example code.

permanent link

answered 26 Feb '14, 22:37

Frederik%20Ramm's gravatar image

Frederik Ramm ♦
82.5k927201273
accept rate: 23%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×25
×9
×8

question asked: 18 Jan '14, 07:20

question was seen: 18,583 times

last updated: 22 Oct '19, 09:14

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum