patternMinor
Google Maps project in JavaScript
Viewed 0 times
javascriptmapsprojectgoogle
Problem
I want you to review my JavaScript project.
```
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
document.upload.lat.value = geoip_latitude();
document.upload.lng.value = geoip_longitude();
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: 'roadmap'
}
map = new google.maps.Map(document.getElementById("gmap"), myOptions);
//var ctaLayer = new google.maps.KmlLayer('http://www.koolbusiness.com/list.kml');
//ctaLayer.setMap(map);
google.maps.event.addListener(map, "click", gAdd);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("message").innerHTML = results[5].formatted_address;
document.upload.place.value = results[5].formatted_address;
} else {
}
});
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var latlng = initialLocation
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent('' + results[1].formatted_address + '');
infowindow.open(map, marker);
document.upload.lat.value = latlng.lat();
```
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
document.upload.lat.value = geoip_latitude();
document.upload.lng.value = geoip_longitude();
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: 'roadmap'
}
map = new google.maps.Map(document.getElementById("gmap"), myOptions);
//var ctaLayer = new google.maps.KmlLayer('http://www.koolbusiness.com/list.kml');
//ctaLayer.setMap(map);
google.maps.event.addListener(map, "click", gAdd);
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("message").innerHTML = results[5].formatted_address;
document.upload.place.value = results[5].formatted_address;
} else {
}
});
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var latlng = initialLocation
geocoder.geocode({
'latLng': latlng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent('' + results[1].formatted_address + '');
infowindow.open(map, marker);
document.upload.lat.value = latlng.lat();
Solution
Comments:
- I highly suggest that you document your functions better. At the beginning of each function, document:
- The overall purpose of the function
- The parameters it takes and their types
- What is returned.
- Document each major block of code.
- Describe the overall procedure for the code piece.
- Document variables that will are already in use that are needed in your code. (I only do this sometimes, it's not as helpful as the rest, but sometimes it's nice)
- Use semicolons for every code statement. At least twice when you declared your variables you didn't use a semicolon at the end.
Context
StackExchange Code Review Q#6157, answer score: 5
Revisions (0)
No revisions yet.