trouble with geoCoderRequest object

55 views
Skip to first unread message

Michael Laird

unread,
Feb 2, 2012, 5:18:49 PM2/2/12
to google-map...@googlegroups.com
This is a JavaScript problem of how to manipulate the LatLng object. I have a two V3 maps running. I want to take the LatLng object from the geocoder.geocode, make it into a string variable and concatenate it into a static map's  src="http:// etc. "  to create a 3rd map - a static map. Currently, I get a solid blue staticmap with a Google logo, which suggests the map is trying to work, but the markerLatLng variable is incorrect.  (Hard coded with Lat and Lng numbers, it works) To remove the outside parenthses of the LatLng object, I have tried toString followed by a pair of replace method calls, and several other things like stringify, all without success. How should this be done? - or do I need to split the object into a Lat string and a Lng string and concatenate them separately? Here's the relevant code with comments showing the content of alerts at several lines. What needs to change?

geocoder.geocode( geoCoderRequest, function(results, status) {   // results[0].geometry.location = (43.098337,-77.441738)
    var markerLatLng = toString(results[0].geometry.location);       //[objectWindow]
    markerLatLng = markerLatLng.replace("(", "").replace(")", "");   //[objectWindow]
    $("#map_canvas").html( '<img src="http://maps.googleapis.com/maps/api/staticmap? etc.'+ 'markerLatLng' + 'etc. ');
    $("#streetAddressGeocode").val( results[0].geometry.location );   //  (43.098337,-77.441738)
});


Michael Laird

unread,
Feb 2, 2012, 5:26:13 PM2/2/12
to google-map...@googlegroups.com
In case I did not make it clear above, if markerLatLng = 43.098337,-77.441738   a string without parentheses, the staticmap will work.

Andrew Leach

unread,
Feb 2, 2012, 5:32:00 PM2/2/12
to google-map...@googlegroups.com
On 2 February 2012 22:18, Michael Laird <mla...@alum.mit.edu> wrote:
> This is a JavaScript problem of how to manipulate the LatLng object. lines.
> What needs to change?

I would definitely go for .lat() and .lng() and use them separately.

Conjecture because I can't verify this at the moment:--
alert(results[0].geometry.location) will display
"(43.098337,-77.441738)" because that's using the [default]
.toString() method of the .location object. However there's no
guarantee that using the *static* toString() method/function will
stringify the object correctly. You might be able to use
results[0].geometry.location.toString() to get what you want; but
.lat() and .lng() are there for the taking without replacing brackets
and the like.

Michael Laird

unread,
Feb 2, 2012, 9:11:43 PM2/2/12
to google-map...@googlegroups.com, andrew....@gmail.com
Andrew Leach,
Thank you.  It works like a charm.
Reply all
Reply to author
Forward
0 new messages