如何使用python将标记经度和经度保存到MySQL数据库表中

时间:2017-05-11 10:02:02

标签: python mysql html5 google-maps-markers latitude-longitude

我有一张带有HTML表单的谷歌地图。用户搜索他的位置并放置标记。单击标记后弹出表单,填写表单并单击提交后,数据将存储到MySQL数据库表中。

但是我也想要用户的位置,即用户放置标记的位置,其纬度和经度要存储到MySQL数据库表中。

我使用Python 2.7作为处理部分,即从表单中将数据插入MySQL数据库。

我正在使用Mac OS Sierra。

我正在使用已预先安装在mac上的Python,MySQL和Apache。

由于我不熟悉编码,可以请某人告诉我我需要添加到我的代码中才能顺利运行。

我正在附加我的HTML以及下面的Python代码:

<!DOCTYPE html>
<html>
   	<head>
	   	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    	<meta charset="utf-8" />
		<title>MAPS</title>

		<style>
			#map {
				width: 100%;
				height: 100%;
			}

			html, body {
			    height: 100%;
			    margin: 0;
			    padding: 0;
			}

			#description {
			    font-family: Calibri;
			    font-size: 15px;
			    font-weight: 300;
			}

			#infowindow-content .title {
			    display: none;
			}

			
			#map #infowindow-content {
			    display: none;
			}

			.pac-card {
			    margin: 10px 10px 0 0;
			    border-radius: 2px 0 0 2px;
			    box-sizing: border-box;
			    -moz-box-sizing: border-box;
			    outline: none;
			    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
			    background-color: #fff;
			    font-family: Calibri;
			}

			#pac-container {
			    padding-bottom: 12px;
			    margin-right: 12px;
			}

			.pac-controls {
			    display: inline-block;
			    padding: 5px 11px;
			}

			.pac-controls label {
			    font-family: Calibri;
			    font-size: 16px;
			    font-weight: 400;
			}

		    #pac-input {
		        background-color: #fff;
		        font-family: Calibri;
		        font-size: 15px;
		        font-weight: 400;
		        margin-left: 12px;
		        padding: 8px 12px;
		        text-overflow: ellipsis;
		        width: 400px;
		        border: 1px solid #ccc;
		    }

		    #pac-input:focus {
		        border-color: #4d90fe;
		    }

		    #title {
		        color: #fff;
		        background-color: #4d90fe;
		        font-size: 25px;
		        font-weight: 400;
		        padding: 6px 12px;
		    }

		    #target {
		    	width: 345px;
		    }

		    #form
			{
				display: none;
			}

		    #heading {
			    text-align: center;
			    color: #003366;
			    font-size: 30px;
			    font-family: Calibri;
			    padding: 8px 12px;
			}

		    label.field {
			    text-align: left;
			    font-weight: bolder;
			    font-size: 16px;
			    font-family: Calibri;
			}

			input[type=text], input[type=email], input[type=number], select {
			   	font-family: Calibri;
			   	font-size: 16px;
			   	font-weight: bolder;
			   	padding: 8px 12px;
			   	width: 100%;
    			margin: 8px 0;
    			display: inline-block;
    			border: 1px solid black;
    			border-radius: 4px;
    			box-sizing: border-box;
			}

			input[type=submit] {
				font-family: Calibri;
				font-size: 16px;
				font-weight: bolder;
    			background-color: white;
    			color: #00b300;
    			padding: 8px 12px;
    			margin: 8px 0;
    			border: 2px solid #00b300;
    			border-radius: 4px;
    			cursor: pointer;
    			box-sizing: border-box;
			}

			input[type=submit]:hover {
    			background-color: #00b300;
    			color: white;
    		}

    		input[type=reset] {
    			font-family: Calibri;
    			font-size: 16px;
    			font-weight: bolder;
    			background-color: white;
    			color: #cc0000;
    			padding: 8px 12px;
    			margin: 8px 0;
    			border: 2px solid #cc0000;
    			border-radius: 4px;
    			cursor: pointer;
    			box-sizing: border-box;
			}

			input[type=reset]:hover {
    			background-color: #cc0000;
    			color: white;
    		}

    		input[type=text]:focus {
    			background-color: #f2f2f2;
    			border-color: #4d90fe;
    		}

    		input[type=email]:focus {
    			background-color: #f2f2f2;
    			border-color: #4d90fe;
    		}

    		input[type=number]:focus {
    			background-color: #f2f2f2;
    			border-color: #4d90fe;
    		}

    		select:focus {
    			background-color: #f2f2f2;
    			border-color: #4d90fe;
    		}
		</style>

		<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC8EAfEQrYVs4c_kMBUQw3tVY8uhEViDCU&libraries=places&callback=initAutocomplete"
                async defer>
        </script> 	
			
	</head>

	<body>
		<div id="map"></div>
		<input id="pac-input" class="controls" type="text" placeholder="Search your location..." />
		<div id="form">
			<form action="/cgi-bin/processing.py" method="post">
    		<p id="heading">PERSONAL INFORMATION</p>
  			<label class="field" for="fullname"><b>Full Name:</b></label>
  			<input type="text" name="fullname" placeholder="Eg. Suraj Makhija" size="30" required />
  			<br />
  			<br />
  			<label class="field" for="emailid"><b>E-mail:</b></label>
  			<input type="email" name="emailid" placeholder="Eg. contact@domain.com" size="30" required />
  			<br />
  			<br />
  			<label class="field" for="mobile"><b>Mobile:</b></label>
  			<input type="text" name="mobile" placeholder="Eg. 9999999999" maxlength="10" minlength="10" size="30" required />
  			<br />
  			<br />
  			<label class="field" for="gender"><b>Gender:</b></label>
  			<select name="gender" required>
    			<option value="male">Male</option>
    			<option value="female">Female</option>
    			<option value="other">Other</option>
  			</select>
  			<br />
  			<br />
  			<label class="field" for="areaofexpertise"><b>Area of Expertise:</b></label>
  			<input type="text" name="areaofexpertise" placeholder="Eg. Android/iOS Developer" size="30" required />
  			<br />
  			<br />
  			<label class="field" for="yearsofexperience"><b>Years of Experience:</b></label>
  			<input type="number" name="yearsofexperience" placeholder="Eg. 10" min="0" max="100" required />
  			<br />
  			<br />
  			<p style="text-align: center;">
  			<input type="submit" value="Submit" /> 
  			<input type="reset" value="Clear All" /> 
  			</p>
        	</form>
        </div>

		<script>
			var map;
			var iconBase;
			var input;
			var searchBox;	 
			var markers=[]; 		
      		var markerArray =[];
      		var infowindow;
      		
		    function initAutocomplete() {
        		map = new google.maps.Map(document.getElementById('map'), {
          			center: {lat: 20.5937, lng: 78.9629},
          			zoom: 5,
         			mapTypeId: 'roadmap',
         			gestureHandling: 'cooperative'
        		});

        	//Declaring infowindow.
        	infowindow = new google.maps.InfoWindow({
          		content: document.getElementById('form')
        	});

            // Create the search box and link it to the UI element.
        	input = document.getElementById('pac-input');
        	searchBox = new google.maps.places.SearchBox(input);
        	map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

            // Bias the SearchBox results towards current map's viewport.
        	map.addListener('bounds_changed', function() {
          		searchBox.setBounds(map.getBounds());
        	});

        	//Creates a marker when the user clicks on a location on the map.
        	google.maps.event.addListener(map, 'click', function(event) {
		        addMarker(event.latLng, map);
		    });

            // Listen for the event fired when the user selects a prediction and retrieve
            // more details for that place.
        	searchBox.addListener('places_changed', function() {
          		var places = searchBox.getPlaces();
          		if (places.length == 0) {
            		return;
         		}

            // Clear out the old markers.
          	markers.forEach(function(marker) {
            	marker.setMap(null);
          	});
          			
            // For each place, get the icon, name and location.
          	var bounds = new google.maps.LatLngBounds();
          	places.forEach(function(place) {
            	if (!place.geometry) {
              		console.log("Returned place contains no geometry");
              		return;
            	}
            var icon = {
              	url: place.icon,
              	size: new google.maps.Size(71, 71),
              	origin: new google.maps.Point(0, 0),
              	anchor: new google.maps.Point(17, 34),
              	scaledSize: new google.maps.Size(25, 25)
            };
			// Create a marker for each place.
		    markers.push(new google.maps.Marker({
		        map: map,
		        icon: icon,
		        title: place.name,
		        position: place.geometry.location
		    }));
		    if (place.geometry.viewport) {
		    // Only geocodes have viewport.
		        bounds.union(place.geometry.viewport);
		        } else {
		        bounds.extend(place.geometry.location);
		        }
		    });
		    map.fitBounds(bounds);
		    });
		}

			//Marker function declaration.
			function addMarker(location, map) {
		    // Add the marker at the clicked location.
		    	var iconBase = 'https://maps.google.com/mapfiles/kml/paddle/';
		        marker = new google.maps.Marker({
		        draggable: true,
	    		animation: google.maps.Animation.DROP,
		        position: location,
		        map: map,
		        icon: iconBase + 'ylw-blank.png'
		    });
		    //Infowindow pops up on clicking the marker.
		    google.maps.event.addListener(marker, 'click', function() {
		    	document.getElementById('form').style.display='inline-block';
            	infowindow.open(map, marker);
          	});
          	var lat = marker.getPosition().lat();
			var lng = marker.getPosition().lng();
		}
		</script>

	</body>

Python代码:

#!/usr/local/bin/python

import cgi

import MySQLdb

import cgitb

cgitb.enable()

form = cgi.FieldStorage()

fullname = form.getvalue('fullname', '')

emailid = form.getvalue('emailid', '')

mobile = form.getvalue('mobile', '')

gender = form.getvalue('gender', '')

areaofexpertise = form.getvalue('areaofexpertise', '')

yearsofexperience = form.getvalue('yearsofexperience', '')

db = MySQLdb.connect(host="localhost", user="root", passwd="suraj", db="client_database")

cursor = db.cursor()

query = """INSERT INTO client_info (FULL_NAME, EMAIL_ID, MOBILE, GENDER, AREA_OF_EXPERTISE, YEARS_OF_EXPERIENCE) 
    VALUES (%s, %s, %s, %s, %s, %s)"""

cursor.execute(query, (fullname, emailid, mobile, gender, areaofexpertise, yearsofexperience))

result = cursor.fetchall()

db.commit()

db.close()

0 个答案:

没有答案