I am developing a web application in django and i want to build a feature where in a person can input a location name in a text box and the corresponding location is shown at the center of a google map on the next page..
Please let me know the detailed way in which this can be done i开发者_如何转开发n Django
Thank you in advance!
I don't know whether this helps you enough, but basically this is one way of doing it:
- Write the form, the view and the template for the location input;
- Read up on how to set up a Google Maps instance with Javascript;
- When a user submits the form, your view calls the Google Maps geocoding web service, using urllib2 (the URL is for instance: http://maps.googleapis.com/maps/api/geocode/json?address=
<address retrieved from form>
&sensor=true - Process the resulting JSON, extracting latitude and longitude;
- Return the lat and lng variables (+ others you might need) to your template;
In your template, where you define your map in javascript, use the lat and lng template variables to set up the map, e.g. (as per the example in the Google Maps JavaScript API docs):
var myLatlng = new google.maps.LatLng({% latitude%}, {% longitude %}); var myOptions = { zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP };
As one commenter noted, your question is very broad - this answer therefore won't go in too many details. If you get the above process working, start expanding on it (you could easily ajaxify this for example, or you could use the GM webservice to return meaningful error messages to the user, ...)
精彩评论