...

View Full Version : How convert address to Lat and Lng from an XML file?



diegomp777
07-10-2012, 11:15 PM
Hi great users !
im new with javascripts.. and i have a problem using the geocoder from a value of the xml file.

this is my script.

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
#map { height: 70% }
</style>
<apputil:css/>
<script type="text/javascript">
var myIcons = [];
myIcons['0'] = 'images/oneclick_flag_green.png';
myIcons['1'] = 'images/oneclick_flag_yellow.png';
myIcons['2'] = 'images/oneclick_flag_brown.png';
myIcons['3'] = 'images/oneclick_flag_orange.png';
myIcons['4'] = 'images/oneclick_flag_red.png';
myIcons['5'] = 'images/oneclick_flag_blue.png';
myIcons['6'] = 'images/oneclick_flag_gray.png';

$(document).ready(function() {

var myLatLng = new google.maps.LatLng(17.74033553, 83.25067267);
MYMAP.init('#map', myLatLng, 11);
MYMAP.placeMarkers('gmaps/markers.xml');
});

var MYMAP = {
map: null,
bounds: null
}

MYMAP.init = function(selector, latLng, zoom) {
var myOptions = {
zoom:zoom,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map($(selector)[0], myOptions);
this.bounds = new google.maps.LatLngBounds();
}

MYMAP.placeMarkers = function(filename) {
$.get(filename, function(xml){
$(xml).find("marker").each(function(){
var name = $(this).find('name').text();
var address = $(this).find('address').text();

// create a new LatLng point for the marker
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var icontype = $(this).find('icon').text();
var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

// extend the bounds to include the new point
MYMAP.bounds.extend(point);

var marker = new google.maps.Marker({
position: point,
icon: myIcons[icontype],
map: MYMAP.map
});

var infoWindow = new google.maps.InfoWindow();
var html='<strong>'+name+'</strong.><br />'+address;
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(MYMAP.map, marker);
});
MYMAP.map.fitBounds(MYMAP.bounds);
});
});
}
</script>


and this is my XML file


<markers>
<marker>
<name>Kenny</name>
<address>Suipacha 400, buenos aires, argentina.</address>
<icon>0</icon>
</marker>
</markers>


how can i convert the ADDRESS to coordinates and then map it !

thanks
diego

xelawho
07-10-2012, 11:30 PM
erm... I don't think you are using the geocoder at all, at least not in that script.

but anyway. if you know the address in advance you shouldn't be geocoding it to get its location on the fly - the geocoder is designed for dynamic input from user generated variables. geocode the address once, get the lat lng and hard code them into your database or page and use that to place the marker

(y saludos desde montevideo :thumbsup:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum