Titanium调用Google服务

Titanium调用Google服务

 

ApplicationWindow.js文件内容:

 

//Application Window Component Constructor
exports.ApplicationWindow = function() {
	var geo = require('geo');
	
	//create object instance
	var self = Ti.UI.createWindow({
		backgroundColor:'#fff',
		fullscreen: false,
		exitOnClose: true
	});

	// create UI components
	var view = Ti.UI.createView({
		backgroundColor: '#800',
		height: '50dp',
		top: 0
	});
	var textfield = Ti.UI.createTextField({
		height: '40dp',
		top: '5dp',
		left: '5dp',
		right: '50dp',
		style: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
		hintText: 'Enter an address',
		backgroundColor: '#fff',
		paddingLeft: '5dp'
	});
	var button = Ti.UI.createButton({
		title: '+',
		font: {
			fontSize: '20dp',
			fontWeight: 'bold'	
		},
		top: '5dp',
		height: '40dp',
		width: '40dp',
		right: '5dp'
	});
	var mapview;
	//Window打开加载事件的使用
	// add map after window opens
	self.addEventListener('open', function() {
		// Make sure we only add the map once
		if (mapview !== undefined) {
			return;	
		}
		
		mapview = Titanium.Map.createView({
		    mapType: Titanium.Map.STANDARD_TYPE,
		    region: {
	    		latitude: geo.LATITUDE_BASE, 
	    		longitude: geo.LONGITUDE_BASE,
		        latitudeDelta: 0.1, 
		        longitudeDelta: 0.1
		    },
		    animate:true,
		    regionFit:true,
		    userLocation:false,
		    top: '50dp'
		});
		
		// Add initial annotation
		mapview.addAnnotation(Ti.Map.createAnnotation({
			animate: true,
			pincolor: Titanium.Map.ANNOTATION_RED,
			title: 'Appcelerator',
			latitude: geo.LATITUDE_BASE,
			longitude: geo.LONGITUDE_BASE,
			leftButton: '/images/delete.png'
		}));
		
		// Handle all map annotation clicks
		mapview.addEventListener('click', function(e) {
			if (e.annotation && (e.clicksource === 'leftButton' || e.clicksource == 'leftPane')) {  
				mapview.removeAnnotation(e.annotation);
			}         
		});
		self.add(mapview);
	});
	
	
	// Execute forward geocode on button click
	button.addEventListener('click', function() {	
		textfield.blur();
		geo.forwardGeocode(textfield.value, function(geodata) {
			mapview.addAnnotation(Ti.Map.createAnnotation({
		    	animate: true,
		    	pincolor: Titanium.Map.ANNOTATION_RED,
		    	title: geodata.title,
		    	latitude: geodata.coords.latitude,
		    	longitude: geodata.coords.longitude,
		    	leftButton: '/images/delete.png'
		    })); 
		    mapview.setLocation({
		    	latitude: geodata.coords.latitude, 
		    	longitude: geodata.coords.longitude,
		        latitudeDelta: 1, 
		        longitudeDelta: 1
		    });
		});
	});
	
	// assemble view hierarchy
	view.add(textfield);
	view.add(button);
	self.add(view);
	
	return self;
};

 

 

geo.js

 

var GOOGLE_BASE_URL = 'http://maps.googleapis.com/maps/geo?output=json&q=';
var ERROR_MESSAGE = 'There was an error geocoding. Please try again.';
exports.LATITUDE_BASE = 37.389569;
exports.LONGITUDE_BASE = -122.050212;

var GeoData = function(title, latitude, longitude) {
	this.title = title;
	this.coords = {
		latitude: latitude,
		longitude: longitude
	};
};
//外部暴露的方法
exports.forwardGeocode = function(address, callback) {
	if (Ti.Platform.osname === 'mobileweb') {
		forwardGeocodeWeb(address, callback);
	} else {
		forwardGeocodeNative(address, callback);
	}
};
//本地通过HttpClient方式访问
var forwardGeocodeNative = function(address, callback) {
	var xhr = Titanium.Network.createHTTPClient();
	xhr.open('GET', GOOGLE_BASE_URL + address);
	xhr.onload = function() {
	    var json = JSON.parse(this.responseText);
	    if (!json.Placemark || !json.Placemark[0].Point || !json.Placemark[0].Point.coordinates) {
	    	alert('Unable to geocode the address');
	    	return;
	    }
	    
	    callback(new GeoData(
	    	address,
	    	json.Placemark[0].Point.coordinates[1],
	    	json.Placemark[0].Point.coordinates[0]
	    ));
	};
	xhr.onerror = function(e) {
		Ti.API.error(e.error);
		alert(ERROR_MESSAGE);
	};
	xhr.send();
};
//Web访问的方式
var forwardGeocodeWeb = function(address, callback) {
	var geocoder = new google.maps.Geocoder();
	if (geocoder) {
      	geocoder.geocode({ 'address': address }, function (results, status) {
        	if (status == google.maps.GeocoderStatus.OK) {
        		callback(new GeoData(
        			address, 
        			results[0].geometry.location.Ua,
        			results[0].geometry.location.Va
        		));
         	} else {
         		Ti.API.error(status);
         		alert(ERROR_MESSAGE); 	
         	}
      	});
	} else {
		alert('Google Maps Geocoder not supported');	
	}
};

你可能感兴趣的:(Titanium,PhoneGap,jquerymobile)