Google Maps

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > Dicas avançadas >

Google Maps

Previous pageReturn to chapter overviewNext page

Tela

 

maps

 

DLL para acessar Google Maps

 

http://googlemaps.subgurim.net/

 

No projeto:

Add Reference...

 

No  Toolbox:

Choose items...

 

Google - pegar a chave 

 

http://code.google.com/intl/pt-BR/

http://code.google.com/intl/pt-BR/apis/maps/signup.html

 

Algo como:

ABQIAAAAa5ZVRBmZcwBY8oSS71jFfxQesgf1aziyCiNnVqW8siJK15P8VhT7O9u8JZl1tkMp6Y5lrF4_qnJs1g

 

Web.config

 

<?xml version="1.0"?>

<configuration>

 <appSettings>

   <add key="googlemaps.subgurim.net" value="ABQIAAAAa5ZVRBmZcwBY8oSS71jFfxQesgf1aziyCiNnVqW8siJK15P8VhT7O9u8JZl1tkMp6Y5lrF4_qnJs1g"/>

 </appSettings>

 

 <system.web>

   <compilation debug="true" targetFramework="4.0"/>

 </system.web>

</configuration>

 

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<%@ Register Assembly="GMaps" Namespace="Subgurim.Controles" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

       Endereço:<br />

      <asp:TextBox ID="txtEndereco" runat="server"></asp:TextBox>

      &nbsp;<asp:Button ID="btnPesquisar" runat="server" OnClick="btnPesquisar_Click" Text="Pesquisar" />

      <br />

      <br />

      <cc1:GMap ID="GMap1" runat="server" Height="400px" Width="600px" />

      <br />

  </div>

  </form>

</body>

</html>

 

Default.aspx.cs

 

using System;

using Subgurim.Controles;

using System.Configuration;

using System.Text;

 

public partial class _Default : System.Web.UI.Page

{

  protected void Page_Load(object sender, EventArgs e)

   {

      // endereço padrão

      if (!IsPostBack)

           txtEndereco.Text = "Rua Campos Sales, 433 - Criciúma, SC";

   }

 

  protected void btnPesquisar_Click(object sender, EventArgs e)

   {

       Pesquisar(txtEndereco.Text);

   }

 

  void Pesquisar(string endereco)

   {

      // não mude o nome da chave (googlemaps.subgurim.net)

      string chave = ConfigurationManager.AppSettings["googlemaps.subgurim.net"].ToString();

 

      // geocode é a localização

      GeoCode gcode = new GeoCode();

 

      // pode vir de um textbox:

       gcode = GMap1.getGeoCodeRequest(endereco, chave);

      GLatLng glatlng = new GLatLng(gcode.Placemark.coordinates.lat, gcode.Placemark.coordinates.lng);

 

      // montar string de informacao

      StringBuilder info = new StringBuilder();

       info.Append("<table><tr><td><img src='http://1.bp.blogspot.com/_yMPNRALenT4/TFb1N4FVNqI/AAAAAAAAAH0/8SeBsqXEoXQ/s1600/mario300.jpg'");

       info.Append("width='50px' height='50px' /> </td>");

       info.Append("<td><i>endereço:<i></br>");

       info.Append("<b>" + endereco + "<b></br>Bem Legal!</br>");

       info.Append("</td></tr></table>");

 

      // balãozinho

      GInfoWindow window = new GInfoWindow(glatlng, info.ToString(), true);

       GMap1.addInfoWindow(window);

 

      // define o que vai aparecer

       GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));

       GMap1.addControl(new GControl(GControl.extraBuilt.NumericalZoomControl));

       GMap1.addControl(new GControl(GControl.preBuilt.MapTypeControl, new GControlPosition(GControlPosition.position.Bottom_Right)));

 

      // seta centro nesta lat/long

       GMap1.setCenter(glatlng, 16, GMapType.GTypes.Normal);

 

      // marcardozinho

      GMarker marker = new GMarker(glatlng);

       GMap1.addGMarker(marker);

 

      // tamanho

       GMap1.Height = 500;

   }

}