Google地圖與Windows C#/Android Java間的溝通
解說與畫面待補
[Form1.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace GoogleMap_1
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "map.html"))
{
Uri uri = new Uri("file:///" + AppDomain.CurrentDomain.BaseDirectory + "map.html");
webBrowser1.Navigate(uri);
webBrowser1.ObjectForScripting = this;
}
}
public void test(String a)
{
MessageBox.Show(a);
}
private void button1_Click(object sender, EventArgs e)
{
Object[] obj = new Object[3];
obj[0] = (Object) Convert.ToDouble(taxilat.Text);
obj[1] = (Object) Convert.ToDouble(taxilog.Text);
obj[2] = (Object)taxiinfor.Text;
//上面的程式將經度與緯度的資料包裝在obj陣列中,以做為參數傳遞給mark函式
webBrowser1.Document.InvokeScript("taxi", obj);//呼叫map.html中的mark函式
}
private void button2_Click(object sender, EventArgs e)
{
Object[] obj = new Object[3];
obj[0] = (Object)Convert.ToDouble(marklat.Text);
obj[1] = (Object)Convert.ToDouble(marklog.Text);
obj[2] = (Object)passengerinfor.Text;
//上面的程式將經度與緯度的資料包裝在obj陣列中,以做為參數傳遞給mark函式
webBrowser1.Document.InvokeScript("passenger", obj);//呼叫map.html中的mark函式
}
}
}
[Form1.cs]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace GoogleMap_1
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "map.html"))
{
Uri uri = new Uri("file:///" + AppDomain.CurrentDomain.BaseDirectory + "map.html");
webBrowser1.Navigate(uri);
webBrowser1.ObjectForScripting = this;
}
}
public void test(String a)
{
MessageBox.Show(a);
}
private void button1_Click(object sender, EventArgs e)
{
Object[] obj = new Object[3];
obj[0] = (Object) Convert.ToDouble(taxilat.Text);
obj[1] = (Object) Convert.ToDouble(taxilog.Text);
obj[2] = (Object)taxiinfor.Text;
//上面的程式將經度與緯度的資料包裝在obj陣列中,以做為參數傳遞給mark函式
webBrowser1.Document.InvokeScript("taxi", obj);//呼叫map.html中的mark函式
}
private void button2_Click(object sender, EventArgs e)
{
Object[] obj = new Object[3];
obj[0] = (Object)Convert.ToDouble(marklat.Text);
obj[1] = (Object)Convert.ToDouble(marklog.Text);
obj[2] = (Object)passengerinfor.Text;
//上面的程式將經度與緯度的資料包裝在obj陣列中,以做為參數傳遞給mark函式
webBrowser1.Document.InvokeScript("passenger", obj);//呼叫map.html中的mark函式
}
}
}
[Map.html]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Marker Animations</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var markers = new Array();
var map;
var image = 'taxi-icon.png'; //image變數指向計程車圖示檔案,圖示檔案請放在相同目錄
function initialize() {
var mapOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(24.155577, 120.679350)
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
// google.maps.event.addListener(map, 'click', find_closest_marker);
}
//計程車標記
function taxi(lat, log, taxiinfor){
var m = new google.maps.LatLng(lat, log);
marker = new google.maps.Marker({
map:map,
draggable:true,
position: m,
icon: image,
title: taxiinfor
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', find_closest_marker);
}
//乘客標記
function passenger(lat, log, custId){
var m = new google.maps.LatLng(lat, log);
marker = new google.maps.Marker({
map:map,
draggable:true,
position: m,
title: custId
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', find_closest_marker);
}
function rad(x) {return x*Math.PI/180;}
function find_closest_marker( event ) {
if (markers.length == 0) return;
var lat = event.latLng.lat();
var lng = event.latLng.lng();
var R = 6371; // radius of earth in km
var distances = [];
var closest = -1;
for( i=0;i<markers.length; i++ ) {
var mlat = markers[i].position.lat();
var mlng = markers[i].position.lng();
var dLat = rad(mlat - lat);
var dLong = rad(mlng - lng);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
distances[i] = d;
if ( closest == -1 || d < distances[closest] ) {
closest = i;
}
}
window.external.test(markers[closest].title);
// alert(markers[closest].title);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width: 800px; height: 800px;">map div</div>
</body>
</html>
Comments