Skip to content

Instantly share code, notes, and snippets.

@jmprado
Created July 7, 2015 17:05
Show Gist options
  • Save jmprado/099afd9fba78280e2440 to your computer and use it in GitHub Desktop.
Save jmprado/099afd9fba78280e2440 to your computer and use it in GitHub Desktop.
Markers.js
var markers;
var address;
var lat;
var lng;
var sleep = 1;
var count = 0;
function initMapCep(cep, num, elm) {
address = getAddr(cep, num);
GMaps.geocode({
address: address,
callback: function (results, status) {
if (status == 'OK') {
var latlng = results[0].geometry.location;
lat = latlng.lat();
lng = latlng.lng();
map = new GMaps({
div: elm,
lat: lat,
lng: lng,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
zoom: 15,
zoomControl: false,
maxZoom: 15
});
map.setCenter(lat, lng);
}
}
});
}
function initMap(address, elm) {
GMaps.geocode({
address: address,
callback: function (results, status) {
if (status == 'OK') {
var latlng = results[0].geometry.location;
lat = latlng.lat();
lng = latlng.lng();
map = new GMaps({
div: elm,
lat: lat,
lng: lng,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
zoom: 15,
zoomControl: false,
maxZoom: 17
});
map.setCenter(lat, lng);
}
}
});
}
function addMarker(address, html) {
if (sleep >= 7) {
sleep = 1;
}
GMaps.geocode({
address: address,
callback: function(results, status) {
if (status == 'OK') {
var latlng = results[0].geometry.location;
if (html == "") {
html = address;
}
lat = latlng.lat();
lng = latlng.lng();
var icon = "/content/images/marker1.png";
map.addMarker({
lat: lat,
lng: lng,
icon: icon,
infoWindow: {
content: html
}
});
count++;
} else {
sleep++;
setTimeout(function() {
addMarker(address, html);
}, sleep * 200);
}
}
});
}
function addMarkerAluguel(address, html) {
if (sleep >= 7) {
sleep = 1;
}
GMaps.geocode({
address: address,
callback: function (results, status) {
if (status == 'OK') {
var latlng = results[0].geometry.location;
if (html == "") {
html = address;
}
lat = latlng.lat();
lng = latlng.lng();
var icon = "/content/images/marker-aluguel.png";
map.addMarker({
lat: lat,
lng: lng,
icon: icon,
infoWindow: {
content: html
}
});
count++;
} else {
sleep++;
setTimeout(function () {
addMarkerAluguel(address, html);
}, sleep * 200);
}
}
});
}
function addMarkerCep(cep, num, html) {
if (sleep >= 10) {
sleep = 1;
}
address = getAddr(cep, num);
GMaps.geocode({
address: address,
callback: function(results, status) {
if (status == 'OK') {
var latlng = results[0].geometry.location;
if (html == "") {
html = address;
}
lat = latlng.lat();
lng = latlng.lng();
var icon = "/content/icons/m1.png";
map.addMarker({
lat: lat,
lng: lng,
icon: icon,
infoWindow: {
content: html
}
});
} else {
sleep++;
setTimeout(function() {
addMarkerCep(cep, num, html);
}, sleep * 200);
}
}
});
}
function getAddr(cep, num) {
var urlJsonCep = 'http://xtends.com.br/webservices/cep/json/' + cep + '/';
var rs = $.parseJSON($.ajax({
url: urlJsonCep,
cep: cep,
async: false
}).responseText);
return rs.logradouro + ', ' + num + ', ' + rs.bairro + ', ' + rs.cidade + ', ' + rs.uf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment