http://www.oracle.com/technology/products/database/application_express/html/doc.html
This Whitepaper explains how to do the geo-coding, i.e. converting an address (Street/City/State/ZIP/Country) into coordinates that can be used to position a Google map. In this example, the PL/SQL package utl_http is used to connect to Google's geocoding service.
An alternative to this approach is to connect to that service from the client. Both techniques have their advantages and disadvantages. One of the advantages of doing it on the client is that the server doesn't need to be able to connect to Google, i.e. there's no need to define any proxies on the server, and as a result, this approach would work on apex.oracle.com, where external network connections are currently not supported.
The first step for any kind of Google map integration is to register for a Google maps key, which can be done here:
http://code.google.com/apis/maps/
After that, it's just a matter of adding some Java Script to your page that communicates with Google and refreshes the area on your APEX page that you have allocated for the map. Here's a link to my sample page:
http://apex.oracle.com/pls/otn/f?p=52790
You can download this application here:
http://www.sewtz.com/GoogleMaps.zip
I added an address region, where you can enter a city or a full address and then click go and have the map move to the address specified. Here's the JavaScript I added to my page. It's pointing the map to my office on first load, and then re-positions the map based on the address you enter:
var geocoder;
var map;
var bounds = new GLatLngBounds();
var myStreet = "540 Madison Ave";
var myCity = "New York";
var myState = "NY";
var myZIP = "10022";
var myCountry = "USA";
var address = myStreet + "," + myCity + "," + myState + "," + myZIP + "," + myCountry;
var addressMarker = myStreet + "<br />" + myCity + ", " + myState + ", " + myZIP + "<br />" + myCountry;
// On page load, call this function
function load()
{
// Create new map object
map = new GMap2(document.getElementById("map"));
// Create new geocoding object
geocoder = new GClientGeocoder();
// Retrieve location information, pass it to addToMap()
geocoder.getLocations(address, addToMap);
}
// This function adds the point to the map
function addToMap(response)
{
// Retrieve the object
place = response.Placemark[0];
// Retrieve the latitude and longitude
point = new GLatLng(place.Point.coordinates[1],
place.Point.coordinates[0]);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
// Center the map on this point
bounds.extend(point);
map.setCenter(point, 13);
map.setZoom(14);
// Create our "tiny" marker icon
var oIcon = new GIcon(G_DEFAULT_ICON);
oIcon.image = "#WORKSPACE_IMAGES#opin.png";
oIcon.iconSize = new GSize(32, 40);
oIcon.shadowSize = new GSize(38, 46);
// Set up our GMarkerOptions object
markerOptions = { icon:oIcon };
marker = new GMarker(point, markerOptions);
// Add the marker to map
map.addOverlay(marker);
// Add address information to marker
marker.openInfoWindowHtml(addressMarker);
}
function updateMap() {
myStreet = $v('P1_STREET');
myCity = $v('P1_CITY');
myState = $v('P1_STATE');
myZIP = $v('P1_ZIP');
myCountry = $v('P1_COUNTRY');
address = myStreet + "," + myCity + "," + myState + "," + myZIP + "," + myCountry;
addressMarker = myStreet + "<br />" + myCity + ", " + myState + ", " + myZIP + "<br />" + myCountry;
load();
}


12 comments:
Marc,
Great post. I've been playing around with this lately but not been able to find the time to build an application that utilizes it.
I think there's an issue with the link you provided to the application on apex.oracle.com.
Regards,
Dan
Thanks for the feedback Dan. I updated the link to the whitepaper, I guess the PDF can't be linked directly, so now I link to our general whitepapers page.
Hi Mark,
thanks a lot for your perfect notes for the community.
Just to stress out dmcghan's comment - the link your apex.oracle.com application is not working. You've probably missread his post :-)
Hi Filip,
Thanks for your comments. I just tried the link to my sample app on apex.oracle.com again, and it worked. It tried from inside the Oracle network and from the outside. So not sure what's causing these issue some readers seem to have. I'll keep an eye an it.
Thanks again,
Marc
Hi Marc.
If I try and access the sample app I get...
Expecting p_company or wwv_flow_company cookie to contain security group id of application owner.
Error ERR-7620 Could not determine workspace for application ().
OK
Marc
I like your post and downloaded the application. I loaded it into a 3.11 environment and attempting to duplicate in a 3.01 environment.
I get the default address to appear however I cannot get the entered address to appear.
Also what are the changes that I need to make to map a set of addresses? Do you have a example of this?
Thanks in advance
I am trying google maps on apex.oracle.com your white paper http://www.oracle.com/technology/products/database/application_express/pdf/Integrating_Application_Express_with_Google_Maps.pdf
But it hangs with error
ORA-29273: HTTP request failed ORA-06512: at "SYS.UTL_HTTP", line 1577 ORA-12535: TNS:operation timed out
Error Calling Geocode Service
can you reply me to nikhilsk99@gmail.com
Hi, Marc
Greatly appreciate your article.
I have a question
If I have a table or view "LIST":
------------------
NO ADDRESS CITY STATE ZIP
1 150 Penn San Jose CA 95123
2 160 Penn San Francisco CA 94123
...
...
------------------
How can I pinpoint all the addresses on the map?
Thank you.
Daniel.
Here are a few replies to some of the comments above. I think the URL issue should now be taken care of, this appears to have been due to the use of an application alias instead of the application ID.
To the question about using this on a different instance, you need to request a Google Maps ID, which you need to register for your domain, and then use that in the application.
To the person asking about the whitepaper, that's no working on apex.oracle.com, because it's using web services, and those require setting a proxy to get out and we can't do that on apex.oracle.com, hence my BLOB entry showing how to do this without requiring a web service.
And lastly, I never tried showing more than one address, but you should be able to find some info on this at Google.
Marc said:
"I think the URL issue should now be taken care of, this appears to have been due to the use of an application alias instead of the application ID."
Does that mean that application aliases no longer work in V3.1.1? I just noticed this problem when we upgraded from V3.0.1. That would be a crying shame, since the aliases make our app more portable. I'm hoping to find a workaround for this (besides using the app ID and page ID).
Hi Marc,
Example is very good.
I have a question if you have an example like this: using an anonymous pl/sql block or using a query to obtain a dynamic distribution of pushpin
on the map ..
Regards,
bogdan
Hi Marc.
It is a very useful application.
I have one question: if I want to pass from oracle form my address, how can I do? Which are parameters that I must add to URL if I want to achieve this result?
Thanks for collaboration, Fabrizio
Post a Comment