简体   繁体   English

静态Google地图在asp.net中不起作用

[英]Static google maps dont work in asp.net

i want to generate a map including large amount of information and lines on a web. 我想生成一张包含大量信息和网络线条的地图。 i try to apply static google maps in which it generates as image and it fast to load.i try to apply the following example but it doesnt desiply the map http://en.googlemaps.subgurim.net/ejemplos/ejemplo_999999_Static_Map.aspx .how can i make it work in asp.net web form? 我尝试应用静态google地图,在该地图中生成图像并快速加载。我尝试应用以下示例,但它并没有描述地图http://en.googlemaps.subgurim.net/ejemplos/ejemplo_999999_Static_Map.aspx 。我怎样才能使其在asp.net Web表单中工作?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="PlantDiseasesSystem.WebForm3" %>
<%@ Register assembly="GMaps" namespace="Subgurim.Controles" tagprefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p>
    <br />
</p>
<cc1:StaticGMap ID="StaticGMap1" runat="server" />
<br />
<br />
<cc1:StaticGMap ID="StaticGMap2" runat="server" />

Ok here is my work. 好的,这是我的工作。 I intended to open a google map in my own web site (not in google) to the user to move a marker on his adress (home, work and for another adresses). 我打算在我自己的网站(而不是google)中向用户打开一个google地图,以在用户的​​地址(房屋,工作地点和其他地址)上移动标记。 and then he/she is recording this marker place to the adress table on the server. 然后他/她将这个标记位置记录到服务器上的地址表中。 when the other user opens his page they can see a "show on the map" link button. 当其他用户打开其页面时,他们可以看到“在地图上显示”链接按钮。 when they click the button explorer opens a google map (if you want you can do it in a div in your self page) and they can see the markers and descriptions about markers. 当他们单击按钮浏览器时,会打开一个Google地图(如果您愿意,可以在自己页面的div中进行操作),他们可以看到标记和有关标记的说明。 I did not design lines in my project but you can ad them with the same way. 我没有在项目中设计线条,但是可以用相同的方式来广告它们。

Another propertie of this code is the user -who adds his/her address- can find the adres with writing his/her address to a text box and uses google finding mechanism. 该代码的另一个特性是,用户-添加其地址-可以通过将其地址写入文本框来找到地址,并使用Google查找机制。 So he/she can place the marker easiliy. 这样他/她可以轻松放置标记。

i used java and vb code. 我用过Java和VB代码。 But the important section handled by javascript code becouse google subgrim is completaly java :) 但是由javascript代码处理的重要部分是因为Google subgrim是完全Java :)

Here is complete javascript code that mentioned. 这里是提到的完整的javascript代码。

var _id = "";
var _adres = "";
var _kull_adi = "";
var _lat;
var _lng;
var geocoder;
var map; 
var marker;
var koords = new Array();

window.onload = function () {
    geocoder = new google.maps.Geocoder();

    var latlng = new google.maps.LatLng(39.42324,35.27587);
    map = new google.maps.Map(document.getElementById('map'), {
        center: latlng,
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: 'Move the marker to the address.',
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function (a) {
        _lat = a.latLng.lat().toFixed(8);
        _lng = a.latLng.lng().toFixed(8);
        $('#mapbilgileri_div').html("Enlem : " + _lat + "</br>Boylam : " + _lng);
    });
};

function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            marker.setMap(null);
            map.setZoom(15);
            map.setCenter(results[0].geometry.location);
            marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: 'İşaretçiyi adresinizin üzerine getiriniz.',
                draggable: true
            });
            $('#mapbilgileri_div').html("Enlem : " + results[0].geometry.location.lat().toFixed(8) + "</br>Boylam : " + results[0].geometry.location.lng().toFixed(8));
            _lat = results[0].geometry.location.lat().toFixed(8);
            _lng = results[0].geometry.location.lng().toFixed(8);
            map.setCenter(marker.position);
            marker.setMap(map);
            google.maps.event.addListener(marker, 'dragend', function (a) {
                _lat = a.latLng.lat().toFixed(8);
                _lng = a.latLng.lng().toFixed(8);
                $('#mapbilgileri_div').html("Enlem : " + _lat + "</br>Boylam : " + _lng);
            });
        } else {
            alert("Address couldn't find. Please be sure that you wrote it tuely!: " + status);
        }
    });
}

function set_guid(guid, adres,kull_adi,adres_decoded) {
    _id = guid;
    _adres = adres;
    _kull_adi = kull_adi;
    $('#address').val(adres_decoded);
    $('#adresbilgileri_div').html(_id + "</br>" + _adres);
    $('#mapbilgileri_div').html("Enlem : " + _lat + "</br>Boylam : " + _lng);
    codeAddress();
}

function koordinatekle() {
    if (_id.length > 3 && _lat != undefined && _lng != undefined && _adres.length > 3) {
        WebService.koordinatekle(_id, _lat, _lng, _kull_adi, geri_donus);
    } else {
        alert('Please select an address and coordinate from the map !')
    }
}

function geri_donus(result, eventArgs) {
    if (result = "1") {
        alert("coordinate added successfully.");
    } else {
        alert("Error!");
    }
    __doPostBack('', '');
}

there is some turkish words in code, think them just as names. 代码中有一些土耳其语单词,就把它们当作名字。 And the web service code: 和网络服务代码:

<WebMethod()> _
    Public Function koordinatekle(ByVal _adres_tipi As String, ByVal _lat As Double, ByVal _lng As Double, ByVal _kull_adi As String) As String
        Try
            Dim _id As Integer = Val(_adres_tipi.Substring(_adres_tipi.IndexOf("-(ID:") + 5))
            If Left(_adres_tipi, 2) = "An" Then
                Dim sorgu = (From p In tablolar.genel_bilgilers Where p.kullanici_adi = _kull_adi Select p).First

                sorgu.lat = _lat
                sorgu.lon = _lng
                tablolar.SubmitChanges()
            End If
            If Left(_adres_tipi, 2) = "Ek" Then
                Dim sorgu = (From p In tablolar.free_texts Where p.id = _id Select p).First

                sorgu.lat = _lat
                sorgu.lon = _lng
                tablolar.SubmitChanges()
            End If
            Return 1
        Catch ex As Exception
            Return ex.Data.ToString
        End Try
    End Function

javascript code calls this service in order to record the selected marker place on a div element. javascript代码调用此服务,以便将选定的标记位置记录在div元素上。

I used jquery dont forget because it eases the works so much. 我用过的jQuery别忘了,因为它大大简化了工作。 First it seems coplicated but very simple. 首先,它看起来很复杂,但是非常简单。 Let me explain it. 让我解释一下。

onload function initialize the map. onload函数初始化地图。 creates a geocoder for finding entered address. 创建用于查找输入地址的地址解析器 creates a map on "map" div element. 在“地图” div元素上创建地图。 creates a marker on this map and sets a listener for marker drag event. 在此地图上创建一个标记,并为标记拖动事件设置一个侦听器。

codeaddress function tries to find the address which is entered by user. codeaddress函数尝试查找用户输入的地址。 the important function of this: 其重要功能:

geocoder.geocode({ 'address': address }, function (results, status) {....

this function sends the adres data to google and try to gets the coordinates of adres. 此函数将adres数据发送到google,并尝试获取adres的坐标。

another functions just sets the data and sends them to the server via a web services. 另一个功能只是设置数据,然后通过Web服务将其发送到服务器。

I hope this can help... 我希望这可以帮助...

if it works don't forget vote me :) thanks. 如果有效,别忘了给我投票:)谢谢。

NOT: I assume that you add the necessary google subgrim keys and necessary dll files (GMaps.dll) to your procet. 不是:我假设您将必需的google subgrim密钥和必需的dll文件(GMaps.dll)添加到了procet。

I've just done this and it worked first time 我刚刚做过,并且第一次成功

WebForm1.aspx WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Website.WebForm1" %>

<%@ Register TagPrefix="cc1" Namespace="Subgurim.Controles" Assembly="GMaps, Version=4.1.0.5, Culture=neutral, PublicKeyToken=564d55b144e7aa5a" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <cc1:StaticGMap ID="StaticGMap1" runat="server" Language="en" format="png32" />
        <br />
        <cc1:StaticGMap ID="StaticGMap2" runat="server" Language="en" format="png32" />
    </form>
</body>
</html>

WebForm1.aspx.cs WebForm1.aspx.cs

public partial class WebForm1 : System.Web.UI.Page
{
    protected override void OnPreLoad(EventArgs e)
    {
        base.OnPreLoad(e);

        LoadMap();
    }

    private void LoadMap()
    {
        GLatLng latLng = new GLatLng(41, -7);

        int colorEnumLength = 11;
        int sizeEnumLength = 3;
        double latStep = -1;
        double lngStep = 0.4;
        string msg = "Subgurim Google Maps";

        int iLat = 0;
        int iLng = 0;

        Random r = new Random();
        foreach (char c in msg)
        {
            if (c == ' ')
            {
                iLat++;
                continue;
            }

            GLatLng latlngAux = latLng + new GLatLng(latStep * iLat + r.NextDouble() * 0.4, lngStep * iLng);

            int randomColor = r.Next(colorEnumLength);
            StaticGMarker.ColorEnum color = (StaticGMarker.ColorEnum)randomColor;

            int randomSize = r.Next(2, sizeEnumLength);
            StaticGMarker.SizeEnum size = (StaticGMarker.SizeEnum)randomSize;

            StaticGMarker staticGMarker = new StaticGMarker(latlngAux, size, color, c);
            StaticGMap1.addStaticGMarker(staticGMarker);

            iLng++;
        }

        StaticPath path = new StaticPath();
        path.colorNet = Color.FromArgb(255, 0, 80);
        path.weight = 5;
        path.alpha = 150;

        path.points.Add(new GLatLng(41, -5));
        path.points.Add(new GLatLng(41, -4));
        path.points.Add(new GLatLng(40, -4));
        path.points.Add(new GLatLng(39, -2));

        StaticGMap1.addStaticPath(path);

        StaticGMap2.setCenter(new GLatLng("Valencia, Spain"));
        GLatLng paris = new GLatLng("Paris");
        GLatLng athens = new GLatLng();
        athens.optionalStringValue = "athens";

        StaticGMap2.addStaticGMarker(new StaticGMarker(paris, new StaticGIcon("http://maps.google.com/mapfiles/kml/shapes/sunny.png")));
        StaticGMap2.addStaticGMarker(new StaticGMarker(athens, new StaticGIcon("http://maps.google.com/mapfiles/kml/shapes/sunny.png", true)));

        StaticPath path2 = new StaticPath();
        path2.fillColorNet = Color.Brown;
        path2.colorNet = Color.ForestGreen;
        path2.weight = 2;
        path2.alpha = 150;
        path2.points.Add(new GLatLng("Berlin"));
        path2.points.Add(new GLatLng("Rome"));
        path2.points.Add(new GLatLng("Morocco"));
        StaticGMap2.Add(path2);
    }
}

Did you reference* the GMap.dll? 您是否参考* GMap.dll?

Did you compile the application? 您是否编译了应用程序?

*Update *更新

EDIT 编辑

I said register, I meant reference. 我说过注册,我指的是参考。 What I would do is place the GMap.dll into a folder called Libs in your solution root (and not in the bin folder) and then in Visual Studio right click on your project and select 'Add Reference...', then browse to the .dll file in the Libs folder, selected it, click ok a few times. 我要做的是将GMap.dll放入解决方案根目录中的Libs文件夹中(而不是bin文件夹中),然后在Visual Studio中右键单击项目并选择“添加引用...”,然后浏览到将Libs文件夹中的.dll文件选中,单击确定几次。 Now build you solution (press the combination Ctrl + Shift + B) 现在构建您的解决方案(按Ctrl + Shift + B组合键)

We really need to see the code from your page though, if you've made a small mistake in copying it, it could break it. 不过,我们确实需要从页面上查看代码,如果您在复制代码时犯了一个小错误,则可能会破坏它。 Otherwise it's like taking your car to be fixed but not letting the mechanic look under the hood! 否则,就好像要修理汽车,而不是让机修工在引擎盖下看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM