How to get latitude and longitude from google maps v3 api?

前端 未结 6 701
孤街浪徒
孤街浪徒 2021-02-04 08:40

I am trying create a map using google maps v3 api. I have found the below code over internet and I want to show the latitude and logitude in map window instead of address.

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 09:19

    When you want the lat and long of any part of the map,try adding the click listeners for the map not the marker. The code should be something like below:

    var infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);
    
        service.getDetails({
            placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
        },function(place,status){
            if(status === google.maps.places.PlacesServiceStatus.OK)
            {
                var marker = new google.maps.Marker({
                    map:map,
                    position: place.geometry.location
    
                });
    
            google.maps.event.addListener(map, 'click', function(e) {
              infowindow.setContent('
    '+'Longitute'+'' + e.latLng.lng() + '
    ' + 'Latitude:'+'' + e.latLng.lat()+'' + '
    '); infowindow.open(map, this); }); } });

    This will display the lat and long of any location on the map using a info window.

提交回复
热议问题