Search code examples
javascriptregexmapsmarkerscloudmade

JS/Regex - Remove surrounding parenthesis; Center marker (CloudMade Maps)


I'm new to programming so the examples that I've seen thus far have been too hard to adopt for my code. I've got a map with a text box above it that shows the current center coordinates of the map. The center coordinates are fetched perfectly but there are parenthesis around them for some reason. I need those parenthesis removed before the form of the input field is submitted.

<div id="coordinates"></div>

<div id="map" style="width:750px;height:400px;">
<script type="text/javascript" src="http://tile.cloudmade.com/wml/latest/web-maps-lite.js"></script>

<script type="text/javascript">
var cloudmade = new CM.Tiles.CloudMade.Web({key: 'YOUR KEY HERE'});
var map = new CM.Map('map', cloudmade);

map.setCenter(new CM.LatLng(38.27269,-92.46094), 4);
var currentLatLng = map.getCenter;
var myMarkerLatLng = new CM.LatLng(38.27269,-92.46094);
var myMarker = new CM.Marker(myMarkerLatLng, {title: "Search a 12-mile radius around this marker..."});
map.addOverlay(myMarker);

CM.Event.addListener(map, 'dragend', function() {
document.getElementById("coordinates").innerHTML = "<input onclick='cleanString();' type='text' name='lat' value='" + map.getCenter().toString(6) + "' />";
});

CM.Event.addListener(map, 'moveend', function() {
document.getElementById("coordinates").innerHTML = "<input type='text' name='lat' value='" + map.getCenter().toString(6) + "' />";
});
</script>

Thanks for any help you can give :)


Solution

  • map.getCenter().toString(6).match(/[^()]+/)
    

    That'll do it as far as the parenthesis go. Can't help you on the marker thing though. When I put your site in a jsfiddle I get a bunch of "status of 403 forbidden" errors.

    basically the above code uses a regex match expression. it's "a match of one or more (+) non-parenthesis ([^()] the ^ symbol is not) characters (the [] indicate a character set)".