Google Maps API 调用实例
本实例介绍如何调用Google Maps API,并实现用鼠标标注地图,保存进数据库,以及二次加载显示等。
1.需要新建一个自定义控件(如:Map.ascx),用于显示Google地图:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Map.ascx.cs" Inherits="StarSoft.UI.Customer.UserControl.Map" %>
<div id="map" style="width: 100%; height: 360px;">
</div>
<table style="display: none">
<tr>
<td width="60px">
<span>
纬度</span>
</td>
<td>
<asp:TextBox ID="txtY" runat="server" CssClass="txtbase" onfocus="this.className='txtfocus';"
onblur="this.className='txtbase';" onpropertychange="javascript:CheckInputFloat(this);"></asp:TextBox>
</td>
<td>
<span>
经度</span>
</td>
<td>
<asp:TextBox ID="txtX" runat="server" CssClass="txtbase" onfocus="this.className='txtfocus';"
onblur="this.className='txtbase';" onpropertychange="javascript:CheckInputFloat(this);"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="4">
<script src=" http://ditu.google.com/maps?file=api&v=2&sensor=false&key=<%=System.Configuration.ConfigurationSettings.AppSettings["googlemapkey"].Trim() %>"
type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function CheckInputFloat(oInput) {
if ('' != oInput.value.replace(/\d{1,}\.{0,1}\d{0,}/, '')) {
oInput.value = oInput.value.match(/\d{1,}\.{0,1}\d{0,}/) == null ? '' : oInput.value.match(/\d{1,}\.{0,1}\d{0,}/);
}
} //<![CDATA[
var x;
var geocoder;
var marker;
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
var LatValue = document.getElementById("<%=txtX.ClientID%>").value;
var LngValue = document.getElementById("<%=txtY.ClientID%>").value;
if ((LatValue == "") || (LngValue == "")) {
map.setCenter(new GLatLng(30.25372, 120.13343), 13);
}
else {
map.setCenter(new GLatLng(LatValue, LngValue), 13);
var curpoint = new GLatLng(LatValue, LngValue);
marker = new GMarker(curpoint, this.ico);
map.addOverlay(marker);
}
geocoder = new GClientGeocoder();
}
function createMarker(point, title, html) {
var marker = new GMarker(point);
//GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(html,{maxContent: html,maxTitle: title});});
return marker;
}
function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
'中国' + address,
function (point) {
if (!point) {
// alert(address + " 未找到");
}
else {
if (marker)//移除上一個點
{
map.removeOverlay(marker);
}
map.setCenter(point, 13);
var title = "地址";
marker = createMarker(point, title, address);
map.addOverlay(marker);
//marker.openInfoWindowHtml(address,{ maxContent: address, maxTitle: title});
}
}
);
}
}
GEvent.addListener(map, 'click', function (overlay, point) {
if (point) {
map.clearOverlays();
var marker = new GMarker(point, { draggable: true });
map.addOverlay(marker); GEvent.addListener(marker, "dragstart", function () {
});
GEvent.addListener(marker, "dragend", function () {
point = marker.getLatLng();
document.getElementById("<%=txtY.ClientID%>").value = point.x;
document.getElementById("<%=txtX.ClientID%>").value = point.y;
});
document.getElementById("<%=txtY.ClientID%>").value = point.x;
document.getElementById("<%=txtX.ClientID%>").value = point.y;
}
});
map.enableScrollWheelZoom();
map.addControl(new GSmallZoomControl());
map.addControl(new GMapTypeControl()); //]]>
</script>
</td>
</tr>
</table>
Map.ascx
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; namespace StarSoft.UI.Customer.UserControl
{
public partial class Map : System.Web.UI.UserControl
{
/// <summary>
/// 纬度
/// </summary>
public string Latitude
{
get { return this.txtX.Text; }
set { this.txtX.Text = value; }
}
/// <summary>
/// 经度
/// </summary>
public string Longitude
{
get { return this.txtY.Text; }
set { this.txtY.Text = value; }
} protected void Page_Load(object sender, EventArgs e)
{ }
}
}
Map.ascx.cs
2.页面中调用这个自定义控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AreaLocation.aspx.cs" Inherits="StarSoft.UI.Customer.AreaLocation" %> <%@ Register src="UserControl/Map.ascx" tagname="Map" tagprefix="uc1" %> <!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 runat="server">
<title>无标题页</title>
<webcontrol:Style ID="Style" runat="server" />
<script language="javascript" type="text/javascript">
function ChooseLoaction()
{
var txtX=document.getElementById('<%=Map1.FindControl("txtX").ClientID %>').value;
var txtY= document.getElementById('<%=Map1.FindControl("txtY").ClientID %>').value;
if(txtX=="")
{
alert('请选择经纬度!');
}
else
{
window.returnVal = txtX+"|"+ txtY;
window.parent.hidePopWin(true);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="popForm" >
<div class="mainForm">
<uc1:Map ID="Map1" runat="server" />
</div></div> <div class="operArea">
<div class="btnArea" >
<input type="button" id="BtnQuery" class="btnSubmit" runat="server" onclick="javascript:ChooseLoaction();"/>
<input type="button" id="close" class="btnReturn" onclick="javascript:parent.hidePopWin(false);" runat="server"/>
</div>
</div>
</form>
</body>
</html>
AreaLocation。aspx
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; namespace StarSoft.UI.Customer
{
public partial class AreaLocation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(StrLoactID) && StrLoactID != "null")
{
Map1.Latitude = StrLoactID.Split(',')[];
Map1.Longitude = StrLoactID.Split(',')[];
} if (!IsPostBack)
{
BtnQuery.Value = StarSoft.Common.ResourceManager.Field("Query");
close.Value = StarSoft.Common.ResourceManager.Field("CancelBtn");
if (Ty != "")
{
close.Value = StarSoft.Common.ResourceManager.Field("CloseBtn");
BtnQuery.Style["display"] = "none";
}
}
} /// <summary>
/// 经纬度信息
/// </summary>
public string StrLoactID
{
get
{
try
{
return Request.Params["LoactID"].ToString();
}
catch
{
return "";
}
}
} /// <summary>
/// 经纬度信息
/// </summary>
public string Ty
{
get
{
try
{
return Request.Params["Ty"].ToString();
}
catch
{
return "";
}
}
}
}
}
AreaLocation。aspx.cs
3.web.config文件中配置Google Map访问秘钥:
<add key="googlemapkey" value="ABQIAAAAFLEnBlXXNVEsCX6NrllENxRtJCFYwXExx0HqCDFUHyWjOHbgXhTNZ_AeqPjv3EmRwdeButm3wRXAuw"/>
运行效果图:

Google Maps API 调用实例的更多相关文章
- Google maps API开发(二)(转)
这一篇主要实现怎么调用Google maps API中的地址解析核心类GClientGeocoder: 主要功能包括地址解析.反向解析.本地搜索.周边搜索等, 我这里主要有两个实例: 实例一.当你搜索 ...
- Google Maps API Web Services
原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...
- Google maps API开发
原文:Google maps API开发 Google maps API开发(一) 最近做一个小东西用到google map,突击了一下,收获不小,把自己学习的一些小例子记录下来吧 一.加载Googl ...
- Google Maps API V3 之绘图库 信息窗口
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google Maps API V3 之 图层
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google Maps API V3 之 路线服务
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- Google maps API开发(一)(转)
一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...
- Google Maps API的使用
之前在学习了简单的API调用后,查看了几个知名网站的API调用方法,发现Google的API调用还是相对比较简单的.下面就从API key的获取.googlemaps的安装,再到实际使用做一下说明. ...
- Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等
extends:http://blog.csdn.net/h7870181/article/details/12505883 Google Maps API 网络服务 官网地址 : https://d ...
- google maps api申请的问题
现在已经改由统一的GOOGLE API控制台进行所有GOOGLE API的管理了. 方法是使用Google帐号登入 https://code.google.com/apis/console. 然后在所 ...
随机推荐
- 为Vell001家族使用过的图标
vell001小海报 最早设计的Vell001图标,根据我偶像Vettel的个人主页上的图片修改而成 由于本人很喜欢jobs,苹果的logo一直是我的最爱 上两个图标已在多个地方使用了,但个人感觉气氛 ...
- Codeforces126B - Password(KMP)
题目大意 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 KMP的失配函数f[i]的非零值就是前i个字符的一个最长前缀且也是后缀的字符 ...
- HDU4745 - Two Rabbits(区间DP)
题目大意 给出一个长度为n的环状序列,两只兔子各自从一个点出发,一个顺时针跳,一个逆时针跳,每个时刻都要求两只兔子所在的数字是相同的,兔子最多跳一个圈~~~问兔子们最多能跳多少次 题解 一个逆时针跳, ...
- dut1305 台阶
Description 如上图所示的一个台阶他的积水量是4 + 2 + 4 + 3 + 4 = 17. 给你一个长度是n的台阶.告诉你每个台阶的高度,求积水量是多少? Input 多组输入数据: 每组 ...
- Topology拓扑
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c
/* Nond-domination based selection routines */ # include <stdio.h> # include <stdlib.h> ...
- 杂谈:你选择coco 还是unity3d?
当一个人喜欢的时候,那么这样的兴趣是非常难改变的.你是否会改变自己想法?眼下而言,如今adobe 对flash开发处于维护的状态.为什么?是由于前期错误政策流失非常多人才,这一点也非常难避免.当今年湖 ...
- 运行时动态修改webconfig
网站运行时,如果直接手动修改webconfig.xml,会造成线程池重启,对于PV大的网站来说风险是很大的,并且如果有多台服务器,逐台修改也会造成数据不一致. .NET 2.0后可以通过代码修改web ...
- 常用加密算法的Java实现(一) ——单向加密算法MD5和SHA
1.Java的安全体系架构 1.1 Java的安全体系架构介绍 Java中为安全框架提供类和接口.JDK 安全 API 是 Java 编程语言的核心 API,位于 java.sec ...
- Java 加密 AES 对称加密算法
版权声明:本文为博主原创文章,未经博主允许不得转载. [AES] 一种对称加密算法,DES的取代者. 加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES R ...