gpt4 book ai didi

Jquery google map api 函数内的函数

转载 作者:行者123 更新时间:2023-12-01 01:46:00 24 4
gpt4 key购买 nike

我正在使用谷歌地图 API v3,并且我有一个包含一些地址的数据库。我有两个脚本可以用来执行此操作。一种用于在 map 上绘制航点,另一种用于将地址转换为纬度和经度。为了绘制航路点,我需要将地址转换为纬度和经度,但我不能在另一个函数中使用一个函数。

路径点脚本:

jQuery(function() {
var stops = [
{"Geometry":{"Latitude":-22.884820,"Longitude":-47.060935}},
{"Geometry":{"Latitude":-22.876644,"Longitude":-47.048631}}
] ;

var map = new window.google.maps.Map(document.getElementById("map"));

// new up complex objects before passing them around
var directionsDisplay = new window.google.maps.DirectionsRenderer({suppressMarkers: true});
var directionsService = new window.google.maps.DirectionsService();

Tour_startUp(stops);

window.tour.loadMap(map, directionsDisplay);
window.tour.fitBounds(map);

if (stops.length > 1)
window.tour.calcRoute(directionsService, directionsDisplay);
});

将地址转换为纬度和经度的函数:

function codeAddress(add) {
geocoder = new google.maps.Geocoder();
var address = add;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {

return "{Geometry:{Latitude:"+results[0].geometry.location.lat() + ",Longitude: "+results[0].geometry.location.lng()}};

}

else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}

最佳答案

这里我使用 google api 给出了 GPS 位置

函数 initMap(emp_id,dateofloc) {

$.ajax({            
url: "GPS.php",
type:"POST",
data:"emp_id="+emp_id+"&dateofloc="+dateofloc,
success: function(result){

var json = $.parseJSON(result);
var i=0;
var locationsdetls='';
var startlat1=0;
var startlat2=0;
var locations = [];
var flightPlanCoordinates = [];
$.each(json , function (index, value){

var res1 = value.MSGText.split(",");
var latitude = res1['0'].split("Lat:");
var longitude = res1['1'].split("Long:");

var latitude =latitude['1'].trim();
var longitude=longitude['1'].trim();

if(i==1)
{
startlat1=latitude;
startlat2=longitude;
}
var local = [];
local[0]=value.address;
local[1]=latitude;
local[2]=longitude;
local[3]=i;
locations[i]=local;

var triangleCoords = new google.maps.LatLng(latitude, longitude);
flightPlanCoordinates.push(triangleCoords);


i++;
});



var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: new google.maps.LatLng(startlat1,startlat2),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow({});

var marker, i;

for (i = 0; i < locations.length; i++) {

marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map


});


google.maps.event.addListener(marker, 'mouseover', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}


var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);


}});

}

关于Jquery google map api 函数内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com