JAVA_HOME
environment variable so that it points to the installation directory.GRAILS_HOME
so that it points to the directory in which the archive was unpacked. Also make sure that the bin/
directory of the Grails installation directory is mentioned in the PATH environment variable.grails help
to check it out. A list of available commands should appear.grails create-app geo_twitter
geo_twitter
application in the current working directory. Go to this directory for all further steps.grails-app/views/layout/main.gsp
and fix it to look like this:<html>
<head>
<title><g:layoutTitle default= "Grails" /></title>
<link rel= "stylesheet" href= "${resource(dir:'css',file:'main.css')}" />
<link rel= "shortcut icon" href= "${resource(dir:'images',file:'favicon.ico')}" type= "image/x-icon" />
<g:layoutHead />
</head>
<body>
<g:layoutBody />
</body>
</html>
grails-app/views/index.gsp
to integrate Google Maps like this:<html>
<head>
<title> Welcome to GeoTwitter! </title>
<meta name= "layout" content= "main" />
<script src= "www.google.com/jsapi?key=YOUR_GOOGLE_MAPS_API_KEY" type= "text/javascript" ></script>
<script type= "text/javascript" >
google.load("maps", "2.x");
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function() {
$(document).ready(function() {
var map = new GMap2(document.getElementById('map'));
var vinnitsa = new GLatLng(49.2325477, 28.4744695); // Replace this by coordinates of your own city ;)
map.setCenter(vinnitsa, 8);
map.addControl(new GLargeMapControl());
});
});
</script>
</head>
<body>
<div id= "map" style= "width:800px; height:600px" >
</div>
</body>
</html>
index.gsp
page.<div class= "form" >
<form action= "" id= "twitter" >
<p>
<label> twitter id: </label>
<input type= "text" id= "name" name= "name" value= "" />
</p>
<p class= "submit" >
<input type= "submit" value= "Map my friends!" >
</p>
</form>
</div>
web-app/css/main.css
with something like this:body {
font-family : Verdana , Helvetica , sans-serif ;
margin : 1em ;
}
#map {
position : absolute ;
width : 800px ;
height : 600px ;
left : 19em ;
top : 1em ;
}
.form {
border : 1px dashed gray ;
width : 15em ;
padding : 0.5em ;
}
.form label {
width : 7em ;
display : block ;
float : left ;
}
.form input {
width : 10em ;
}
.form .submit {
padding-left : 7em ;
}
grails install-plugin twitter
grails create-controller Twitter
grails-app/controllers/TwitterController.groovy
file with the controller skeleton. It should be replaced with a controller implementation that will display information about friends in JSON format. He will also contact the geocoding service to get coordinates on the map by the name of the area.import grails.converters.*
class TwitterController {
// Google Maps API key
static def API_KEY = "Insert your Google Maps API key here"
// TwitterService instance will be injected into this variable by Spring
def twitterService
def friendsJson = {
// Get friends of given user
def friends = getFriends ( params . name )
// Render friends list as JSON
render ( friends as JSON )
}
private def getFriends ( String userName ) {
def friends = twitterService . getFriends ( params . name )
// Return only the needed fields for each user and retrieve coordinates for location
friends . collect { it ->
[
screenName: it . screenName ,
name: it . name ,
pictureUrl: it . profileImageUrl as String ,
bio: it . description ,
status: it . status ?. text ,
coords: getCoordsFromLocation ( it . location )
]
}
}
/**
* This method gets coordinates on map for given location string.
*/
private def getCoordsFromLocation ( String location ) {
if ( location ) {
if ( location . contains ( "iPhone:" )) {
// There can be coords specified in location
// like iPhone: 39.035248,-77.138687
location = location . replace ( "iPhone: " , "" )
def parts = location . split ( "," )
return [ latitude: parts [0], longitude: parts [1]]
} else {
// Encode location as URL
def encodedLocation = URLEncoder . encode ( location )
// Call web service by retrieving URL content
def response =
"maps.google.com/maps/geo?q=${encodedLocation}&output=xml&key=${API_KEY}" . toURL (). getText ()
// Parse response XML
def root = new XmlSlurper (). parseText ( response )
if ( root . Response . Placemark . size () == 1) {
def coords = root . Response . Placemark . Point . coordinates . text ()
def parts = coords . split ( "," )
if ( parts . size () > 1) {
return [ latitude: parts [1] as Double , longitude: parts [0] as Double ]
}
}
}
}
// No coordinates are determined
return null
}
}
action="${createLink(controller: 'twitter', action: 'friendsJson'}"
index.gsp
, like:google.load( "maps" , "2.x" );
google.load( "jquery" , "1.3.1" );
google.setOnLoadCallback( function () {
$( document ).ready( function () {
// Create and configure Google Map control
var map = new GMap2( document .getElementById( "map" ));
var vinnitsa = new GLatLng( 49.2325477 , 28.4744695 );
map.setCenter(vinnitsa, 4 );
map.addControl( new GLargeMapControl());
// Add form submit handler
var form = $( "#twitter" );
form.submit( function () {
$.getJSON(form.attr( "action" ) + "?" + form.serialize(), function (data) {
// Clear all markers
map.clearOverlays();
// Loop through friends list and add markers to map
$.each(data, function (i, item) {
if (item.coords) {
var marker = new GMarker( new GLatLng(item.coords.latitude, item.coords.longitude));
map.addOverlay(marker);
var popup = '<img style="width: 48px; height:48px;" src="' + item.pictureUrl + '">' +
item.name + ' (' + item.screenName + ') <br>' +
item.bio + '<br>' + item.status;
GEvent.addListener(marker, "click" , function () {
marker.openInfoWindowHtml(popup);
});
}
});
});
// Indicate that form should not actually be submitted
return false ;
});
});
});
Source: https://habr.com/ru/post/75774/
All Articles