Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 115d881

Browse files
committed
Initial version with Mendix 5 features like snippets.
1 parent eee1f4f commit 115d881

File tree

6 files changed

+200
-1
lines changed

6 files changed

+200
-1
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/proxies/
2+
src/javasource/com
3+
src/javasource/system
4+
src/deployment/
5+
src/.classpath
6+
src/.project
7+
*.launch
8+
*.tmp
9+
*.lock

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# GoogleMapsModule
2-
Module utilizing the googlemaps api to determine coordinates based on an address.
2+
Module utilizing the Google Maps api to determine coordinates based on an address. For licensing and terms using this Google API, please refer to the [Terms of Service](https://developers.google.com/maps/terms)
3+
4+
## Contributing
5+
For more information on contributing to this repository visit [Contributing to a GitHub repository](https://world.mendix.com/display/howto50/Contributing+to+a+GitHub+repository)!
6+
7+
## Typical usage scenario
8+
Determine latitude and longitude based on an address. With these coordinates it becomes possible to view a location on a Google map, using the Mendix Google Maps Widget.
9+
10+
## Configuration
11+
The module contains snippets and example pages which you can use in your project. The example pages are excluded from the project by default in order to reduce the dependency on layouts (which might not be available in your project).
12+
You can find the Snippets in the '_Use me' folder in the project navigation. You can find the example pages in '_Example pages' folder in the project navigation.
13+
14+
#### Snippets
15+
Copy the snippets which you would like to use to a page in your project.
16+
17+
#### Example pages
18+
Copy the example pages which you would like to use in your project to a module in your project structure. (E.g MyFirstModule) Include the page in your project by right-clicking the page in the project navigation and clicking 'Include in project'.
19+
20+
21+
22+

src/Google Maps Widget.mpr

546 KB
Binary file not shown.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// This file was generated by Mendix Business Modeler.
2+
//
3+
// WARNING: Only the following code will be retained when actions are regenerated:
4+
// - the import list
5+
// - the code between BEGIN USER CODE and END USER CODE
6+
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
7+
// Other code you write will be lost the next time you deploy the project.
8+
// Special characters, e.g., é, ö, à, etc. are supported in comments.
9+
10+
package googlemaps.actions;
11+
12+
import com.mendix.core.Core;
13+
import com.mendix.systemwideinterfaces.core.UserAction;
14+
import googlemaps.actions.GeoCoder.Location;
15+
import java.io.IOException;
16+
import java.security.AccessControlException;
17+
import com.mendix.systemwideinterfaces.core.IMendixObject;
18+
import com.mendix.systemwideinterfaces.core.IContext;
19+
import com.mendix.webui.CustomJavaAction;
20+
21+
/**
22+
*
23+
*/
24+
public class CalculateGeo extends CustomJavaAction<Boolean>
25+
{
26+
private IMendixObject __locationObj;
27+
private googlemaps.proxies.Location locationObj;
28+
private String addressString;
29+
30+
public CalculateGeo(IContext context, IMendixObject locationObj, String addressString)
31+
{
32+
super(context);
33+
this.__locationObj = locationObj;
34+
this.addressString = addressString;
35+
}
36+
37+
@Override
38+
public Boolean executeAction() throws Exception
39+
{
40+
this.locationObj = __locationObj == null ? null : googlemaps.proxies.Location.initialize(getContext(), __locationObj);
41+
42+
// BEGIN USER CODE
43+
Location loc;
44+
45+
loc = GeoCoder.getLocation(addressString);
46+
47+
locationObj.setLatitude(loc.lat);
48+
locationObj.setLongitude(loc.lon);
49+
50+
return true;
51+
// END USER CODE
52+
}
53+
54+
/**
55+
* Returns a string representation of this action
56+
*/
57+
@Override
58+
public String toString()
59+
{
60+
return "CalculateGeo";
61+
}
62+
63+
// BEGIN EXTRA CODE
64+
// END EXTRA CODE
65+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package googlemaps.actions;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.URL;
7+
import java.net.URLEncoder;
8+
import java.security.AccessControlException;
9+
10+
import org.json.JSONArray;
11+
import org.json.JSONObject;
12+
13+
public class GeoCoder {
14+
private final static String ENCODING = "UTF-8";
15+
private final static String STATUS_OK = "OK"; //indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
16+
private final static String STATUS_ZERO_RESULTS ="ZERO_RESULTS"; //indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
17+
private final static String STATUS_OVER_QUERY_LIMIT ="OVER_QUERY_LIMIT"; //indicates that you are over your quota.
18+
private final static String STATUS_REQUEST_DENIED ="REQUEST_DENIED"; //indicates that your request was denied, generally because of lack of a sensor parameter.
19+
private final static String STATUS_INVALID_REQUEST ="INVALID_REQUEST"; //generally indicates that the query (address or latlng) is missing.
20+
private final static String STATUS_UNKNOWN_ERROR = "UNKNOWN_ERROR"; //indicates that the request could not be processed due to a server error. The request may succeed if you try again.
21+
22+
23+
public static class Location {
24+
public Double lon, lat;
25+
26+
private Location (Double lat, Double lon) {
27+
this.lon = lon;
28+
this.lat = lat;
29+
}
30+
31+
public String toString() {
32+
return "Lat: "+lat+", Lon: "+lon;
33+
}
34+
}
35+
36+
public static Location getLocation (String address) throws IOException {
37+
38+
StringBuilder sb = new StringBuilder();
39+
Location location = null;
40+
41+
42+
BufferedReader in = new BufferedReader (
43+
new InputStreamReader (
44+
new URL ("https://maps.googleapis.com/maps/api/geocode/json?address="+URLEncoder.encode (address, ENCODING)+"&sensor=false")
45+
.openStream ()
46+
)
47+
);
48+
String line;
49+
50+
int statusCode = -1;
51+
while ((line = in.readLine ()) != null) {
52+
sb.append(line);
53+
}
54+
55+
in.close();
56+
57+
58+
JSONObject json = new JSONObject(sb.toString());
59+
60+
JSONArray jsonresults = (json.getJSONArray("results"));
61+
62+
String jsonstatus = (json.getString("status"));
63+
64+
if(jsonstatus.equals(STATUS_OK))
65+
{
66+
67+
if(jsonresults.length()>0)
68+
{
69+
70+
JSONObject jsongeometry = (jsonresults.getJSONObject(0).getJSONObject("geometry"));
71+
72+
if(!jsongeometry.equals(null))
73+
{
74+
JSONObject jsonlocation = (jsongeometry.getJSONObject("location"));
75+
76+
location = new Location (
77+
(jsonlocation.getDouble("lat")),
78+
(jsonlocation.getDouble("lng")));
79+
}
80+
}
81+
}
82+
else{
83+
if(jsonstatus.equals(STATUS_OVER_QUERY_LIMIT)) throw new IOException("GEOCODE request failed: you are over your quota");
84+
else if(jsonstatus.equals(STATUS_ZERO_RESULTS)) throw new IOException("The geocode request was successful but returned no results");
85+
else if(jsonstatus.equals(STATUS_REQUEST_DENIED)) throw new IOException("The geocode request is denied");
86+
else if(jsonstatus.equals(STATUS_INVALID_REQUEST)) throw new IOException("The geocode request is invalid (generally indicates that the query (address or latlng) is missing)");
87+
else if(jsonstatus.equals(STATUS_UNKNOWN_ERROR)) throw new IOException("The geocode request was not succesfull due to a google server error. The request may succeed if you try again.");
88+
}
89+
90+
if (location == null) {
91+
switch (statusCode) {
92+
case 400: throw new IOException ("Bad Request");
93+
case 500: throw new IOException ("Unknown error from Google Encoder");
94+
case 601: throw new IOException ("Missing query");
95+
case 602: throw new IOException ("Address could not be found");
96+
case 603: throw new IOException ("Legal problem");
97+
case 604: throw new IOException ("No route");
98+
case 610: throw new IOException ("Bad key");
99+
case 620: throw new IOException ("Too many queries");
100+
}
101+
}
102+
return location;
103+
104+
}
105+
}

src/widgets/GoogleMaps.mpk

7.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)