45973

How do I get a pin on Google Maps using location from a variable? [closed]

Question:

I want to place the pins on my Google Maps map with a variable containing the location, when I make one like this, my pin works.

var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(62.446026, 17.331340); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,

But I want to use this method, to store the latitude and longitude inside a variable:

var location = "62.446026, 17.331340"; var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(location); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,

When I do as the second snippet, the pin just disappear. What is causing this?

Thanks

Answer1:

The google.maps.LatLng() function doesn't take string parameters. It takes the following parameters:

lat : number lng : number noWrap : boolean // Optional

This is documented here: <a href="https://developers.google.com/maps/documentation/javascript/reference#LatLng" rel="nofollow">https://developers.google.com/maps/documentation/javascript/reference#LatLng</a>

You could store the latitude and longitude this way:

var location = { lat: 62.446026, lng: 17.331340 }; var imageDriver = 'marker.png'; var DriverLatLng = new google.maps.LatLng(location.lat, location.lng); var driverMarker = new google.maps.Marker({ position: DriverLatLng, map: map, icon: imageDriver,

Answer2:

google.maps.LatLng() requires 2 numeric values, you're trying to initialize it with a string.

You could split the string into an array and then parse both strings inside the array as float for long and lat value:

location = location.split(','); var DriverLatLng = new google.maps.LatLng(parseFloat(location[0]),parseFloat(location[1])); ....

Answer3:

The problem is that you're supplying your LatLng constructor in the second instance with a string. This isn't correct and won't work - this is why you're not seeing the pin. In the first instance, you're giving it two numbers which is good. You can see the documentation here: <a href="https://developers.google.com/maps/documentation/javascript/reference#LatLng" rel="nofollow">https://developers.google.com/maps/documentation/javascript/reference#LatLng</a> To do what you're trying to do, you could have two variables (which are numbers):<br />

var locationLat = 62.446026; var locationLng = 17.331340; var DriverLatLng = new google.maps.LatLng(locationLat,locationLng); <br /> ...or you could do something like they've done in this answer: <a href="https://stackoverflow.com/questions/6778061/convert-string-to-latlng-google-maps" rel="nofollow">Convert String to latlng google maps</a>, if you're determined to keep your latitude and longitude in a string.<br /> ...or you could create a new object and assign the latlng to it if you wanted a single variable:var myLatLng = new Object(); myLatLng.lat = 62.446026; myLatLng.lng = 17.331340; <br /> ...or, having constructed the google LatLng object, you could get the lat and lng out of it later like so: var myLatLng = new google.maps.LatLng(62.446026, 17.331340); // some other stuff here var myStringLatLng = myLatLng.lat() + ',' + myLatLng.lng(); <br /> I guess the best option depends on what you're trying to do!

Recommend

  • How to make a messagebox disappear after 3 seconds?
  • UITableView cell goes blank when scrolling
  • What is the fastest way to select nearest geographical place from mysql database?
  • Android Cursor RecyclerView does a flick on swipe
  • Tokenize python source code examples (in Python)
  • Fixed positioned elements disappear on page unload
  • How do you open .txt file containing matrix in Matlab?
  • XGBOOST - DMATRIX
  • Getting EOFError along with exceptions when using ftplib
  • How to Divide an array on c#?
  • Google map in top corner left
  • Criterion causing memory consumption to explode, no CAFs in sight
  • Azure webjobs output logs indexing taking very long
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Parse a date string in a specific locale (not timezone!)
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • How do I change content of ComboFieldEditor?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • JSON with duplicate key names losing information when parsed
  • Trying to switch camera back to front but getting exception
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • AT Commands to Send SMS not working in Windows 8.1
  • Compare two NSDates in iPhone
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • SQL merge duplicate rows and join values that are different
  • Free memory of cv::Mat loaded using FileStorage API
  • C# - Getting references of reference
  • Angular 2 constructor injection vs direct access
  • apache spark aggregate function using min value
  • Programmatically clearing map cache
  • Sorting a 2D array using the second column C++
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?