google.maps.event.addlistener和window.external vb.net [英] google.maps.event.addlistener and window.external vb.net

查看:78
本文介绍了google.maps.event.addlistener和window.external vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些我正在使用的代码。我不是一个优秀的程序员,我是一名具有一些编程知识的地质学家...所以任何帮助都会受到赞赏。



我有一个webbrowser控件它显示了使用Google Maps API的HTML代码。我需要做的是从我点击webbrowser控件的地方提取坐标(lat-lng)。



我正在使用VB.NET使用VS2015和.NET Framework 4.5.2



HTML代码为:

Hi, I have some problems with some code that I'm working. I'm not a good programmer, I'm a geologist with a few knowledge of programming... So any help would be appreciated.

I have a webbrowser control that shows a html code using Google Maps API. What I need to do is to extract the coordinates (lat-lng) from the place I've clicked on the webbrowser control.

I'm ussing VB.NET with VS2015 and .NET Framework 4.5.2

The HTML code is:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Poligonal - Vista previa</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script type="text/javascript">

var latitud
var longitud
var latylong

function coordGoogle(str) {
 window.external.coordGoogle(str);
}   

function Map_Click(e){
    latitud = e.latLng.lng();
    longitud = e.latLng.lng(); 
    latylong =  latitud + " " + longitud;
    coordGoogle(latylong);
}

function initMap() {
  var bounds = new google.maps.LatLngBounds();
  bounds.extend(new google.maps.LatLng(-57.9273407169989, -34.9455780254582));
  bounds.extend(new google.maps.LatLng(-57.9280791179909, -34.9463018545704));
  var nuevoZoom = getBoundsZoomLevel(bounds, 630, 740);

  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: nuevoZoom,
      tilt: 0,
      mapTypeId: "hybrid",
      center: {lat: -34.945932129038, lng: -57.9277009575209 }
  });

  setMarkers(map);

google.maps.event.addListener(map, 'click', Map_Click);

}

// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.
var puntos = [["E1", -34.9456863703928, -57.9279809728952, 0],["E2", -34.9455780254582, -57.9276547821795, 1],["E3", -34.9461597123083, -57.9273407169989, 2],["E4", -34.9463018545704, -57.9275503647045, 3],["E5", -34.9459350794102, -57.9279876151491, 4],["E1'", -34.9457038384157, -57.9280344515735, 5]];

//Esta funcion ajusta el zoom a los datos
//Extraido de http://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calculate-the-zoom-level-for-a-given-bounds

function getBoundsZoomLevel(bounds, mapHeight, mapWidth) {
    var WORLD_DIM = { height: 256, width: 256 };
    var ZOOM_MAX = 21;

    function latRad(lat) {
        var sin = Math.sin(lat * Math.PI / 180);
        var radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
        return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
    }

    function zoom(mapPx, worldPx, fraction) {
        return Math.floor(Math.log(mapPx / worldPx / fraction) / Math.LN2);
    }

    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var latFraction = (latRad(ne.lat()) - latRad(sw.lat())) / Math.PI;

    var lngDiff = ne.lng() - sw.lng();
    var lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;

    var latZoom = zoom(mapHeight, WORLD_DIM.height, latFraction);
    var lngZoom = zoom(mapWidth, WORLD_DIM.width, lngFraction);

    return Math.min(latZoom, lngZoom, ZOOM_MAX);
}

function setMarkers(map) {
  // Adds markers to the map.

    for (var i = 0; i < puntos.length; i++) {
        var punto = puntos[i];
    var marker = new google.maps.Marker({
        position: { lat: punto[1], lng: punto[2] },
      map: map,
      icon: new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + punto[0] + "|FC6254|000000"),
      title: punto[0],
      zIndex: punto[3]
    });
  }
}

</script>
    <script async defer
        src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>
  </body>
</html>





VB.NET代码是:



And the VB.NET code is:

Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
<System.Runtime.InteropServices.ComVisibleAttribute(True)>
Public Class mainForm
    
    Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.ObjectForScripting = Me
    End Sub

    Public Sub coordGoogle(ByVal latlng As String)
        MsgBox(latlng)
    End Sub

End Class





因此,当我点击webbrowser时,没有任何反应。 coordGoogle子网不会运行。我根本不明白有什么不对。如果我将HTML代码保存为html文件并且我使用通用浏览器打开(我使用Firefox),我可以调试代码,我看到lat-lng字符串被传递给coordGoogle函数,所以我不知道为什么 coordGooglesub不运行。



你能帮我解决这个问题吗?谢谢!



joaco



PS:对不起我的英语



So, when I click on the webbrowser, nothing happens. The "coordGoogle" sub does not run. I simply don't understand what is wrong. If I save the HTML code as html file and I open with a common browser (I use Firefox), I can debug the code and I see that lat-lng string is passed to coordGoogle function, so I don't know why the "coordGoogle" sub does not run.

Can you help me with this? Thanks!

joaco

PS: sorry for my english

推荐答案

最后,我发现代码出了什么问题!在VB中,有一个子程序可以生成javascript代码的某些部分,特别是两个变量叫做puntos和puntos2。在原帖上我简化了javascript代码以便更好地阅读,认为这段代码很好......



puntos2变量最后有逗号和额外的逗号,这个额外的逗号阻止了window.external进程。



所以,最终的javascript代码是:

Finally, I found what was wrong with the code! In VB, there is a subroutine that generates some parts of javascript code, especifycally two variables called "puntos" and "puntos2". On the original post I simplified the javascript code for better reading, thinking that this code was fine...

The "puntos2" variable have and extra comma at the end, and this extra comma was blocking the window.external process.

So, the final javascript code is this:
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Poligonal - Vista previa</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </meta></meta></head>
  <body>
    <div id="map"></div>
    <script type="text/javascript">

var latitud
var longitud
var latylong

function coordGoogle(str) {
 window.external.coordGoogle(str);
}   
 
function Map_Click(e){
    latitud = e.latLng.lat();
    longitud = e.latLng.lng(); 
    latylong =  latitud + " " + longitud;
    coordGoogle(latylong);
}

function initMap() {
  var bounds = new google.maps.LatLngBounds();
  bounds.extend(new google.maps.LatLng(-57.9273407169989, -34.9455780254582));
  bounds.extend(new google.maps.LatLng(-57.9280791179909, -34.9463018545704));
  var nuevoZoom = getBoundsZoomLevel(bounds, 630, 740);

  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: nuevoZoom,
      tilt: 0,
      mapTypeId: "hybrid",
      center: {lat: -34.945932129038, lng: -57.9277009575209 }
  });

  

  setMarkers(map);
  setMarkers2(map);

  google.maps.event.addListener(map, 'click', Map_Click);

}

// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.
var puntos = [["E1", -34.9456863703928, -57.9279809728952, 0],["E2", -34.9455780254582, -57.9276547821795, 1],["E3", -34.9461597123083, -57.9273407169989, 2],["E4", -34.9463018545704, -57.9275503647045, 3],["E5", -34.9459350794102, -57.9279876151491, 4],["E1'", -34.9457038384157, -57.9280344515735, 5]];
var puntos2 = [["1", -34.9458646104288, -57.9277684856222, 1],["2", -34.9458301555346, -57.9276848953222, 2],["3", -34.9456424949709, -57.9278302365446, 3],["4", -34.9456477344085, -57.9277152389542, 5],["5", -34.945722286851, -57.9276564071297, 6],["6", -34.9457875635967, -57.927589118614, 7],["7", -34.9460946833933, -57.9274513093932, 9],["8", -34.9461634761396, -57.9273884621752, 10],["9", -34.9461678758456, -57.9274442738932, 11],["11", -34.9462280721616, -57.9274975162208, 13],["12", -34.9459700858821, -57.9276543326257, 14],["22", -34.9462239845672, -57.927710713715, 15],["14", -34.9459218009585, -57.9278793037058, 17],["15", -34.9459862149622, -57.9278217925306, 18],["17", -34.9459587895424, -57.9280791179909, 19]];

//Esta funcion ajusta el zoom a los datos
//Extraido de http://stackoverflow.com/questions/6048975/google-maps-v3-how-to-calculate-the-zoom-level-for-a-given-bounds

function getBoundsZoomLevel(bounds, mapHeight, mapWidth) {
    var WORLD_DIM = { height: 256, width: 256 };
    var ZOOM_MAX = 21;

    function latRad(lat) {
        var sin = Math.sin(lat * Math.PI / 180);
        var radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
        return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
    }

    function zoom(mapPx, worldPx, fraction) {
        return Math.floor(Math.log(mapPx / worldPx / fraction) / Math.LN2);
    }

    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var latFraction = (latRad(ne.lat()) - latRad(sw.lat())) / Math.PI;

    var lngDiff = ne.lng() - sw.lng();
    var lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;

    var latZoom = zoom(mapHeight, WORLD_DIM.height, latFraction);
    var lngZoom = zoom(mapWidth, WORLD_DIM.width, lngFraction);

    return Math.min(latZoom, lngZoom, ZOOM_MAX);
}

function setMarkers(map) {
  // Adds markers to the map.

    for (var i = 0; i < puntos.length; i++) {
        var punto = puntos[i];
    var marker = new google.maps.Marker({
        position: { lat: punto[1], lng: punto[2] },
      map: map,
      icon: new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + punto[0] + "|FC6254|000000"),
      title: punto[0],
      zIndex: punto[3]
    });
  }
}

function setMarkers2(map) {
  // Adds markers to the map.

    for (var i = 0; i < puntos2.length; i++) {
        var punto = puntos2[i];
    var marker = new google.maps.Marker({
        position: { lat: punto[1], lng: punto[2] },
      map: map,
      icon: new google.maps.MarkerImage("http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png"),
      title: punto[0],
      zIndex: punto[3]
    });
  }
}

    </script>
    <script async="" mode="hold" />        src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap">
  </body>
</html>





puntos2的结尾就像...... 19,];现在是.... 19]];。我觉得很蠢。抱歉浪费你的时间!



谢谢



joaco



The end of "puntos2" was like "....19],];" and now is "....19]];". I feel so stupid. And sorry for waste your time!

Thanks

joaco


这篇关于google.maps.event.addlistener和window.external vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆