001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui.input.location; 029 030import java.util.ArrayList; 031import java.util.List; 032 033import com.google.gwt.core.client.JavaScriptObject; 034import com.google.gwt.user.client.ui.SuggestOracle; 035 036/** 037 * A suggest oracle for locations based on the google maps API.<p> 038 */ 039public class CmsLocationSuggestOracle extends SuggestOracle { 040 041 /** 042 * The location suggestion data.<p> 043 */ 044 public static class LocationSuggestion implements SuggestOracle.Suggestion { 045 046 /** The address. */ 047 private String m_address; 048 049 /** 050 * Constructor.<p> 051 * 052 * @param address the address 053 */ 054 public LocationSuggestion(String address) { 055 056 m_address = address; 057 } 058 059 /** 060 * @see com.google.gwt.user.client.ui.SuggestOracle.Suggestion#getDisplayString() 061 */ 062 public String getDisplayString() { 063 064 return m_address; 065 } 066 067 /** 068 * @see com.google.gwt.user.client.ui.SuggestOracle.Suggestion#getReplacementString() 069 */ 070 public String getReplacementString() { 071 072 return m_address; 073 } 074 075 } 076 077 /** The location controller. */ 078 private CmsLocationController m_controller; 079 080 /** The google geocoder instance. */ 081 private JavaScriptObject m_geocoder; 082 083 /** 084 * Constructor.<p> 085 * 086 * @param controller the location controller 087 */ 088 public CmsLocationSuggestOracle(CmsLocationController controller) { 089 090 m_controller = controller; 091 } 092 093 /** 094 * Adds a location suggestion to the list.<p> 095 * 096 * @param suggestions the suggestions list 097 * @param address the address 098 */ 099 private static void addSuggestion(List<LocationSuggestion> suggestions, String address) { 100 101 suggestions.add(new LocationSuggestion(address)); 102 } 103 104 /** 105 * Creates a location suggestions list.<p> 106 * 107 * @return the location suggestions list 108 */ 109 private static List<LocationSuggestion> createSuggestList() { 110 111 return new ArrayList<LocationSuggestion>(); 112 } 113 114 /** 115 * Executes the suggestions callback.<p> 116 * 117 * @param request the suggestions request 118 * @param suggestions the suggestions 119 * @param callback the callback 120 */ 121 private static void respond(Request request, List<LocationSuggestion> suggestions, Callback callback) { 122 123 callback.onSuggestionsReady(request, new Response(suggestions)); 124 } 125 126 /** 127 * @see com.google.gwt.user.client.ui.SuggestOracle#requestSuggestions(com.google.gwt.user.client.ui.SuggestOracle.Request, com.google.gwt.user.client.ui.SuggestOracle.Callback) 128 */ 129 @Override 130 public native void requestSuggestions(final Request request, final Callback callback) /*-{ 131 var query = request.@com.google.gwt.user.client.ui.SuggestOracle.Request::getQuery()(); 132 var controller = this.@org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::m_controller; 133 var pos = controller.@org.opencms.gwt.client.ui.input.location.CmsLocationController::getCurrentPosition()(); 134 if (this.@org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::m_geocoder == null) { 135 this.@org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::m_geocoder = new $wnd.google.maps.places.AutocompleteService(); 136 } 137 this.@org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::m_geocoder 138 .getPlacePredictions( 139 { 140 'input' : query, 141 'location' : pos, 142 'radius' : 10000 143 }, 144 function(results, status) { 145 var suggestions = @org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::createSuggestList()(); 146 // check to see if we have at least one valid address 147 if (results 148 && (status == $wnd.google.maps.places.PlacesServiceStatus.OK)) { 149 for (var i = 0; i < results.length; i++) { 150 var lat = 0; 151 //results[i].geometry.location.lat(); 152 var lng = 0; 153 //results[i].geometry.location.lng(); 154 var address = results[i].description; 155 //results[i].formatted_address; 156 @org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::addSuggestion(Ljava/util/List;Ljava/lang/String;)(suggestions, address); 157 } 158 } 159 @org.opencms.gwt.client.ui.input.location.CmsLocationSuggestOracle::respond(Lcom/google/gwt/user/client/ui/SuggestOracle$Request;Ljava/util/List;Lcom/google/gwt/user/client/ui/SuggestOracle$Callback;)(request, suggestions, callback); 160 }); 161 }-*/; 162 163}