0

i am using google map.i have a simple example which is working fine.here is the code

function initialize() {
        if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById('map_canvas'));
            map.setCenter(new GLatLng(1, 103.849), 5);
            map.addOverlay(new ELabel(new GLatLng(1, 103.849), 1, 'style11'));  
            map.addOverlay(new ELabel(new GLatLng(1, 103.980), 2, 'style11'));
            map.setUIToDefault();}}

this above function is working well.Now i want to make it dynamic.like to set center i want to pass variable , like i place of this i will like to use this var center="1, 103.849" and i place of this line map.addOverlay(new ELabel(new GLatLng(1, 103.849), 1, 'style11')); i want to use this line in variable.... here is my code which do not work.can anybody correct me?

function initialize() {
        if (GBrowserIsCompatible()) {
            var center = "1, 103.849";
            var Locations = "map.addOverlay(new ELabel(new GLatLng(1, 103.849), 1, 'style11'));map.addOverlay(new ELabel(new GLatLng(1, 103.980), 2, 'style11'));";

            var map = new GMap2(document.getElementById('map_canvas'));
            map.setCenter(new GLatLng(center), 5);
             +Locations +

            map.setUIToDefault();
        }
    }
Methew
  • 359
  • 4
  • 10
  • 28

1 Answers1

1

GLatLng takes two numbers as arguments, not a string. You need to do

var centerlat = 1;
var centerlng = 103.849;
map.setCenter(new GLatLng(centerlat,centerlng), 5);

I'm not sure what +Locations+ is designed to achieve. It's a syntax error. Just use the map.addOverlay() line you had before.

map.addOverlay(new ELabel(new GLatLng(centerlat,centerlng), 2, 'style11'));

Note that Version 2 is deprecated and doesn't have long to live. Use Version 3 of the API (which is rather different).

Andrew Leach
  • 12,945
  • 1
  • 40
  • 47
  • yes i am using version 3.yes i have fixed this point map.setCenter(new GLatLng(centerlat,centerlng), 5);....the issue is still exist in next point....i am pointing differect locations on googlemap what is why creating map.addOverlay dynamically for all records...for example if i have to show 50 points on map then we have to use 50 map.addOverlay... – Methew Apr 30 '12 at 12:09
  • i edit my code now...here is issue when i want more than one data on google me...actually i want to display all the search results user have searched – Methew Apr 30 '12 at 12:13
  • 1
    You are not using Version 3. `GBrowserIsCompatible`, `GMap2` and `GLatLng` are all Version 2; and `ELabel` only works with Version 2. – Andrew Leach Apr 30 '12 at 13:17
  • Can any body help me what to use instead?i have html,css and js to create numbered markers on map.like 1,2,3 etc – Methew May 01 '12 at 05:41
  • @Methew: That's a different question. And there have already been a couple of SO questions on that too. [See Google](http://www.google.co.uk/search?q=google+javascript+api+numbered+markers) – Andrew Leach May 01 '12 at 06:45
  • As you can see ELable gets a parameter of css which make markers for use dynamically...is there any way to do so...on this link the icons are static which i dnt want...http://stackoverflow.com/questions/2436484/how-can-i-create-numbered-map-markers-in-google-maps-v3 – Methew May 01 '12 at 08:29