-1

I've checked all over the internet and I have tried some solutions but can't seem to find out how to combine your own location and at the same time show some markers on the map.

I got this code to show multiple markers and it works great but how do I call my own location with google maps as well? I know there is documentation on how to add your own location but not to combine these two options.

Edit regarding duplicates:

There is no question like this on stackoverflow and is therefore no duplicate as there is no question regarding combining multiple markers and show your own location marker.

Here's the code I got right now:

     <script>

        function initialize() {
            var map;
            var bounds = new google.maps.LatLngBounds();
            var mapOptions = {
                mapTypeId: 'roadmap'
            };




            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            map.setTilt(45);


            var markers = [
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567],
    ['some locations, city', 52.35475,19.532567]
            ];




            var infoWindowContent = [
                ['<div class="info_content">' +
                '<h3>London Eye</h3>' +
                '<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>' +        '</div>'],
                ['<div class="info_content">' +
                '<h3>Palace of Westminster</h3>' +
                '<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' +
                '</div>']
            ];




            var infoWindow = new google.maps.InfoWindow(), marker, i;

           // Here we get our own location

    var showPosition = function (position) {
    var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    var marker = new google.maps.Marker({
        position: userLatLng,
        title: 'Your Location',
        map: map
    });
}

    // Show all the markers and show my own location. Show my own location don't work.

    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2], showPosition); 
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });


                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infoWindow.setContent(infoWindowContent[i][0]);
                        infoWindow.open(map, marker);
                    }
                })(marker, i));


                map.fitBounds(bounds);
            }


            var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
                this.setZoom(11);
                google.maps.event.removeListener(boundsListener);
            });

        }
        </script>
Adam Norbäcker
  • 91
  • 1
  • 10
  • Possible duplicate of [Multiple markers Google Map API v3 from array of addresses and avoid OVER\_QUERY\_LIMIT while geocoding on pageLoad](http://stackoverflow.com/questions/19640055/multiple-markers-google-map-api-v3-from-array-of-addresses-and-avoid-over-query) – roberrrt-s Sep 21 '16 at 08:13
  • It's not a duplicate... That one doesn't show it's own location as well. – Adam Norbäcker Sep 21 '16 at 08:21
  • 1
    Ah, can't you just push your own location in the array as an array? – roberrrt-s Sep 21 '16 at 08:23
  • Unfortunately I have no idea how... something like this? ['some locations, city', 52.35475,19.532567], [(position.coords.latitude, position.coords.longitude)] – Adam Norbäcker Sep 21 '16 at 08:30
  • `var myLocation = ['some locations, city', position.coords.latitude, position.coords.longitude]; markers.push(myLocation);` – roberrrt-s Sep 21 '16 at 08:34
  • Thanks but it's causing the map to be gray. I got a codepen here:http://codepen.io/Adamnorbacker/pen/xERzLR – Adam Norbäcker Sep 21 '16 at 08:57
  • It's partly because you're using an unsecure origin. I'm answering your question below, and that code should work on localhost or https. – roberrrt-s Sep 21 '16 at 09:07
  • 1
    It looks like variable position is undefined in your code. There is an error message in console. I think you should use [HTML5 Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation). Also have a look at this [article](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation). – xomena Sep 21 '16 at 09:09
  • Thank you xomena, Can't get it to work yet tho. – Adam Norbäcker Sep 21 '16 at 09:30
  • Why downvote? The question is stated clearly and it's also clearly stated that it ain't a duplicate. – Adam Norbäcker Sep 21 '16 at 09:31
  • Duplicate of [Google maps multiple markers with geolocation button](http://stackoverflow.com/questions/33982038/google-maps-multiple-markers-with-geolocation-button) – geocodezip Sep 21 '16 at 09:31
  • Oh haven't seen this one, however, It doesn't show a marker of where you are. Only zooms in to location. – Adam Norbäcker Sep 21 '16 at 09:38
  • See the javascript console: getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details. – geocodezip Sep 21 '16 at 09:43
  • Actually, I managed to solve it geocodezip. It apparently worked on your code. Here's the code/ fiddle you provided with some edits: https://jsfiddle.net/k4kqg1gw/10/ I would uprate it if I had enough repetition but It seems I can't get to the rep limit by some odd reasons. Also, both codepen and jsfiddle has a https domain – Adam Norbäcker Sep 21 '16 at 09:44

2 Answers2

1

Add the following JavaScript code below your declaration of var markers

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(callback) {
            showPosition(callback)
        }, function(error) {
            console.log(error)
        });
    }
}

function showPosition(position) {
    markers.push(['test', position.coords.latitude, position.coords.longitude])
}

getLocation();

We're grabbing the user location, and pushing it in the initial array of array's that hold the data for the markers, please note that this doesn't work on http, because Google discourages the usage of the getCurrentPosition() by disabling it on unsecure origins. Localhost should work, as it is considered 'safe-ish'

roberrrt-s
  • 7,914
  • 2
  • 46
  • 57
  • Hmm really nice of you, but can you verify that it works? my ampps localhost does not show my own location/marker but it asks for it. here's the codepen, the same code I have in my localhost: https://codepen.io/Adamnorbacker/pen/xERzLR – Adam Norbäcker Sep 21 '16 at 09:24
  • You'll have to allow your browser to get your location ofc. – roberrrt-s Sep 21 '16 at 09:24
  • It asks for my location and i allow it too. hmm, tried on safari and firefox. I appreciate your help :) – Adam Norbäcker Sep 21 '16 at 09:25
1

Made it by using geocodezips answer on: Google maps multiple markers with geolocation button

Didn't have marker but managed to add it myself by some code Roberrrt provided before. Here's the jsfiddle: https://jsfiddle.net/k4kqg1gw/27/

['Helooo',40, 30 ,0] , ['Helooo',41.04564,28.97862 ,1], ['Your location',position.coords.latitude, position.coords.longitude] , ];
Community
  • 1
  • 1
Adam Norbäcker
  • 91
  • 1
  • 10