|
15 | 15 |
|
16 | 16 | package com.google.maps; |
17 | 17 |
|
| 18 | +import com.google.gson.Gson; |
| 19 | +import com.google.maps.model.CellTower; |
18 | 20 | import com.google.maps.model.GeolocationPayload; |
| 21 | +import com.google.maps.model.GeolocationPayload.GeolocationPayloadBuilder; |
19 | 22 | import com.google.maps.model.GeolocationResult; |
| 23 | +import com.google.maps.model.WifiAccessPoint; |
20 | 24 |
|
21 | 25 | /** |
22 | 26 | * Request for the Geolocation API. |
23 | 27 | */ |
24 | 28 | public class GeolocationApiRequest |
25 | 29 | extends PendingResultBase<GeolocationResult, GeolocationApiRequest, GeolocationApi.Response>{ |
26 | 30 |
|
27 | | - private GeolocationPayload payload; |
| 31 | + private GeolocationPayload payload = null; |
| 32 | + private GeolocationPayloadBuilder builder = null; |
| 33 | + |
28 | 34 |
|
29 | 35 | GeolocationApiRequest(GeoApiContext context) { |
30 | 36 | super(context, GeolocationApi.GEOLOCATION_API_CONFIG, GeolocationApi.Response.class); |
| 37 | + builder = new GeolocationPayload.GeolocationPayloadBuilder(); |
31 | 38 | } |
32 | 39 |
|
33 | 40 | @Override |
34 | 41 | protected void validateRequest() { |
35 | | - // TODO: see DirectionsApiRequest for an example on how to validate |
| 42 | + if(this.payload.considerIp != null |
| 43 | + && this.payload.considerIp == false |
| 44 | + && this.payload.wifiAccessPoints != null |
| 45 | + && this.payload.wifiAccessPoints.length < 2) { |
| 46 | + throw new IllegalArgumentException("Request must contain two or more 'Wifi Access Points'"); |
| 47 | + } |
| 48 | + } |
| 49 | + public GeolocationApiRequest HomeMobileCountryCode(int newHomeMobileCountryCode) |
| 50 | + { |
| 51 | + this.builder.HomeMobileCountryCode(newHomeMobileCountryCode); |
| 52 | + return this; |
| 53 | + } |
| 54 | + public GeolocationApiRequest HomeMobileNetworkCode(int newHomeMobileNetworkCode) |
| 55 | + { |
| 56 | + this.builder.HomeMobileNetworkCode(newHomeMobileNetworkCode); |
| 57 | + return this; |
| 58 | + } |
| 59 | + public GeolocationApiRequest RadioType(String newRadioType) |
| 60 | + { |
| 61 | + this.builder.RadioType(newRadioType); |
| 62 | + return this; |
| 63 | + } |
| 64 | + public GeolocationApiRequest Carrier(String newCarrier) |
| 65 | + { |
| 66 | + this.builder.Carrier(newCarrier); |
| 67 | + return this; |
| 68 | + } |
| 69 | + public GeolocationApiRequest ConsiderIp(boolean newConsiderIp) |
| 70 | + { |
| 71 | + this.builder.ConsiderIp(newConsiderIp); |
| 72 | + return this; |
| 73 | + } |
| 74 | + public GeolocationApiRequest CellTowers(CellTower[] newCellTowers) |
| 75 | + { |
| 76 | + this.builder.CellTowers(newCellTowers); |
| 77 | + return this; |
| 78 | + } |
| 79 | + public GeolocationApiRequest AddCellTower(CellTower newCellTower) |
| 80 | + { |
| 81 | + this.builder.AddCellTower(newCellTower); |
| 82 | + return this; |
| 83 | + } |
| 84 | + public GeolocationApiRequest WifiAccessPoints(WifiAccessPoint[] newWifiAccessPoints) |
| 85 | + { |
| 86 | + this.builder.WifiAccessPoints(newWifiAccessPoints); |
| 87 | + return this; |
| 88 | + } |
| 89 | + public GeolocationApiRequest AddWifiAccessPoint(WifiAccessPoint newWifiAccessPoint) |
| 90 | + { |
| 91 | + this.builder.AddWifiAccessPoint(newWifiAccessPoint); |
| 92 | + return this; |
| 93 | + } |
| 94 | + public GeolocationApiRequest Payload(GeolocationPayload payload) |
| 95 | + { |
| 96 | + this.payload = payload; |
| 97 | + return this; |
| 98 | + } |
| 99 | + public GeolocationApiRequest CreatePayload() |
| 100 | + { |
| 101 | + if(this.payload == null) { |
| 102 | + // if the payload has not been set, create it |
| 103 | + this.payload = this.builder.createGeolocationPayload(); |
| 104 | + } else { |
| 105 | + // use the payload that has been explicitly set by the Payload method above |
| 106 | + } |
| 107 | + Gson gson = new Gson(); |
| 108 | + String jsonPayload = gson.toJson(this.payload); |
| 109 | + return param("_payload", jsonPayload); |
36 | 110 | } |
37 | 111 | } |
0 commit comments