iefMap = new google.maps.ImageMapType(
{
getTileUrl: function(coord, zoom)
{
coord = getNormalizedCoord(coord, zoom);
if (!coord) return null;
var y = coord.y, x = coord.x;
return 'http://maps-for-free.com/layer/relief/z'+zoom+'/row'+y+'/'+zoom+'_'+x+'-'+y+'.jpg';
},
tileSize: new google.maps.Size(256, 256),
maxZoom: 11,
minZoom: 0,
// radius: 1738000,
name: 'Elevation'
});
// map.mapTypes.set('relief', ReliefMap);
// /**
// * @constructor
// * @implements {google.maps.MapType}
// */
// function TopoMap() {
// this.tileSize = new google.maps.Size(256, 256)
// }
// TopoMap.prototype.maxZoom = 14;
// TopoMap.prototype.name = 'Relief';
// TopoMap.prototype.alt = 'Tile Relief Map Type';
// TopoMap.prototype.getTile = function(coord, zoom, ownerDocument) {
// var div = ownerDocument.createElement('div');
// if (zoom > this.maxZoom) return null;
// div.style.width = this.tileSize.width + 'px';
// div.style.height = this.tileSize.height + 'px';
// div.style.backgroundImage = "url("+this.getTileUrl(coord,zoom)+")";
// div.style.opacity = 0.3;
// return div;
// };
// TopoMap.prototype.getTileUrl = function(coord, zoom) {
// coord = getNormalizedCoord(coord, zoom);
// if (!coord) return null;
// var y = coord.y, x = coord.x;
// return 'https://a.tile.opentopomap.org/'+zoom+'/'+x+'/'+y+'.png';
// };
var freeTopoMap = new google.maps.ImageMapType({
getTileUrl: function(coord, zoom)
{
coord = getNormalizedCoord(coord, zoom);
if (!coord) return null;
var y = coord.y, x = coord.x;
return 'https://a.tile.opentopomap.org/'+zoom+'/'+x+'/'+y+'.png';
},
tileSize: new google.maps.Size(256, 256),
maxZoom: 14,
minZoom: 0,
// radius: 1738000,
name: 'Topographic'
});
map.mapTypes.set('topo', freeTopoMap);
map.setMapTypeId('topo');
if (window['defaultMarker'])
{
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: defaultMarker
});
}
if (window['defaultViewport'] && defaultViewport.match(/\d/))
{
var arr = defaultViewport.split(',');
var ne = new google.maps.LatLng(arr[0], arr[1]);
var sw = new google.maps.LatLng(arr[2], arr[3]);
var bounds = new google.maps.LatLngBounds(sw, ne);
map.fitBounds(bounds);
}
var info = new google.maps.InfoWindow({content: '',position: myLatlng, foo:true});
var __timer;
google.maps.event.addListener(map, 'click', function(evt)
{
__timer = setTimeout(function()
{
showInfo(evt.latLng);
}, 500);
});
google.maps.event.addListener(map, 'dblclick', function(evt)
{
try { clearTimeout(__timer); } catch(e) {}
});
// $('#map-address').bind('keypress',function(evt)
// {
// if(evt.keyCode == 13) $('#map-search-btn').trigger('click');
// });
var pac = new google.maps.places.Autocomplete($('#map-address').get(0));
pac.addListener('place_changed', function(evt)
{
var place = pac.getPlace();
if (!place.geometry)
{
if ($('#map-address').val()) $('#map-search-btn').trigger('click');
return;
}
var name = place.name;
var place_id = place.place_id;
var myPt = place.geometry.location;
if (place.geometry.bounds)
{
map.fitBounds(place.geometry.bounds);
}
map.setCenter(myPt);
showInfo(myPt,name, true, place_id);
$('#map-address').val('');
});
$('#map-search-btn').bind('click',function()
{
var address = $('#map-address').val();
if (!address ) return;
$('#search-address-options').html('loading...').show();
var g = new google.maps.Geocoder();
g.geocode(
{
address: address
//region:'zh-CN'
}, function(data)
{
if (!data || data.length == 0)
{
alert('No Results!');
return;
}
//console.log(data);
$('#search-address-options').html('Please select:').show();
for(var i=0,d; d=data[i];i++)
{
var pt = d.geometry.location;
$('')
.html(d.formatted_address)
.attr('data-lat',pt.lat())
.attr('data-lng',pt.lng())
.data('bounds', d.geometry.viewport)
.data('place_id', d.place_id)
.appendTo($('#search-address-options'));
}
if (data.length == 1)
{
$('#search-address-options').find('div').trigger('click');
}
});
});
$('#search-address-options').delegate('div[data-lat][data-lng]','click',function()
{
var lat = $(this).attr('data-lat');
var lng = $(this).attr('data-lng');
var bounds = $(this).data('bounds');
var place_id = $(this).data('place_id');
var name = $(this).html();
$('#search-address-options').hide();
var myPt = new google.maps.LatLng(lat,lng);
map.setCenter(myPt);
map.fitBounds(bounds);
//map.setZoom(map.getZoom()+1);
if (bounds)
{
var d = bounds.toJSON();
bounds = [d.north,d.east,d.south,d.west].join(',');
}
showInfo(myPt,name, true,place_id, bounds);
});
if (window['haibaPoiFixed'] !== undefined && haibaPoiFixed == 0)
{
setTimeout(function()
{
showInfo(myLatlng,defaultMarker,true,defaultPlaceId);
},100);
}
function showInfo(myPt,title,save,place_id, bounds)
{
$.cookie('last_lat', myPt.lat(), {expires:365});
$.cookie('last_lng', myPt.lng(), {expires:365});
$.cookie('last_zoom', map.getZoom(), {expires:365});
if (!title)
{
var geocoder = new google.maps.Geocoder({language:'zh-cn'});
geocoder.geocode({'latLng': myPt}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results && results.length > 0)
{
//console.log(results);
var title = results[0].formatted_address || '';
$('#google-maps-popup-info-title').html(title);
}
}
});
title = '';
}
info.open(map);
info.setPosition(myPt);
info.setContent('Lng:'+myPt.lng()+'
Lat:'+myPt.lat()+'
Elevation: ...
');
$('#map-lng').html(myPt.lng());
$('#map-lat').html(myPt.lat());
$.get('/?p=get_elevation&pt=' + myPt.lat()+','+myPt.lng(), function(data)
{
var data = JSON.parse(data);
console.log(data);
console.log(data.results[0].elevation);
if (data && data.results && data.results[0])
{
var alt = Math.round(data.results[0].elevation);
var altFeet = Math.round(alt*3.28083);
$('#map-alt').html( alt+'m / '+altFeet+'feet');
$('#pendding-alt-number').html(alt+'m / '+altFeet+'feet');
if (save && title)
{
$.post('/index.php?p=haiba&action=save',
{
// lat:myPt.lat(),
// lng:myPt.lng(),
// name: title,
// alt: alt,
// viewport: bounds,
// haiba_id: window['defaultHaibaId'],
// map_level: map.getZoom(),
place_id: place_id
},function(data)
{
var url = data;
if (url && url.match(/\d+\.html$/))
{
$('#google-maps-popup-info-title').each(function()
{
//var title = $(this).html();
var $a = $(''+title+'').attr('href', url);
$(this).html('').append($a);
});
}
});
}
}
});
}
});
});
function getNormalizedCoord(coord, zoom)
{
var y = coord.y;
var x = coord.x;
// tile range in one direction range is dependent on zoom level
// 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
var tileRange = 1 << zoom;
// don't repeat across y-axis (vertically)
if (y < 0 || y >= tileRange) {
return null;
}
// repeat across x-axis
if (x < 0 || x >= tileRange) {
x = (x % tileRange + tileRange) % tileRange;
}
return {x: x, y: y};
}