簡體   English   中英

靜態Google地圖在asp.net中不起作用

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

我想生成一張包含大量信息和網絡線條的地圖。 我嘗試應用靜態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" />

好的,這是我的工作。 我打算在我自己的網站(而不是google)中向用戶打開一個google地圖,以在用戶的​​地址(房屋,工作地點和其他地址)上移動標記。 然后他/她將這個標記位置記錄到服務器上的地址表中。 當其他用戶打開其頁面時,他們可以看到“在地圖上顯示”鏈接按鈕。 當他們單擊按鈕瀏覽器時,會打開一個Google地圖(如果您願意,可以在自己頁面的div中進行操作),他們可以看到標記和有關標記的說明。 我沒有在項目中設計線條,但是可以用相同的方式來廣告它們。

該代碼的另一個特性是,用戶-添加其地址-可以通過將其地址寫入文本框來找到地址,並使用Google查找機制。 這樣他/她可以輕松放置標記。

我用過Java和VB代碼。 但是由javascript代碼處理的重要部分是因為Google subgrim是完全Java :)

這里是提到的完整的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('', '');
}

代碼中有一些土耳其語單詞,就把它們當作名字。 和網絡服務代碼:

<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代碼調用此服務,以便將選定的標記位置記錄在div元素上。

我用過的jQuery別忘了,因為它大大簡化了工作。 首先,它看起來很復雜,但是非常簡單。 讓我解釋一下。

onload函數初始化地圖。 創建用於查找輸入地址的地址解析器 在“地圖” div元素上創建地圖。 在此地圖上創建一個標記,並為標記拖動事件設置一個偵聽器。

codeaddress函數嘗試查找用戶輸入的地址。 其重要功能:

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

此函數將adres數據發送到google,並嘗試獲取adres的坐標。

另一個功能只是設置數據,然后通過Web服務將其發送到服務器。

我希望這可以幫助...

如果有效,別忘了給我投票:)謝謝。

不是:我假設您將必需的google subgrim密鑰和必需的dll文件(GMaps.dll)添加到了procet。

我剛剛做過,並且第一次成功

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

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);
    }
}

您是否參考* GMap.dll?

您是否編譯了應用程序?

*更新

編輯

我說過注冊,我指的是參考。 我要做的是將GMap.dll放入解決方案根目錄中的Libs文件夾中(而不是bin文件夾中),然后在Visual Studio中右鍵單擊項目並選擇“添加引用...”,然后瀏覽到將Libs文件夾中的.dll文件選中,單擊確定幾次。 現在構建您的解決方案(按Ctrl + Shift + B組合鍵)

不過,我們確實需要從頁面上查看代碼,如果您在復制代碼時犯了一個小錯誤,則可能會破壞它。 否則,就好像要修理汽車,而不是讓機修工在引擎蓋下看!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM