Adding marker on Google Maps using a button

ic90

I'm trying to add a marker using a button in Google Maps and I'm getting the error, cannot read property lat of null. I'm not that good in Javascript, any help?

 function setmarker(){
        setLatLng = new google.maps.LatLng(document.getElementById('latitude').value,document.getElementById('longitude').value); 
        alert(setLatLng);
        var map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);

        var mapOptions = {
        zoom: 8,
        //center: new google.maps.LatLng(-1.2921897,36.8288574)
      };


        var marker = new google.maps.Marker({
          position: setLatLng,
          map: map,
          title: 'Obtained coordinate'
        });

    }
    </script>
  </head>
  <body>
<div id="map-canvas"></div>


<div class="side-panel">
  <div>Latitude:<br><input type="text" name="latitude" id="latitude" ></div>
  <div>Longitude:<br><input type="text" name="longitude" id="longitude"></div><br>
  <!-- <input type="submit" onsubmit="setmarker();"> -->
  <button onclick="setmarker()">Show Marker</button>
</div>
ic90

Thanks for all the answers, stumbled upon the answer in the name of Reverse GeoCoding Service from Google that exactly did what I wanted to quite easily and in the exact way. From the API: https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(40.730885,-73.997383);
  var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: 'roadmap'
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeLatLng() {
  var input = document.getElementById('latlng').value;
  var latlngStr = input.split(',', 2);
  var lat = parseFloat(latlngStr[0]);
  var lng = parseFloat(latlngStr[1]);
  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        map.setZoom(11);
        marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
        infowindow.setContent(results[1].formatted_address);
        infowindow.open(map, marker);
      } else {
        alert('No results found');
      }
    } else {
      alert('Geocoder failed due to: ' + status);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Making Google maps marker array global breaks marker click event

분류에서Dev

Restrict google maps API marker to only one click per marker

분류에서Dev

Google Maps Android Undo Marker Drag

분류에서Dev

Parse JSON response for Google Maps Marker in Android

분류에서Dev

how to get the coordinates of google maps marker and store it in textbox or label using asp.net c#

분류에서Dev

@ react-google-maps Marker LatLng throwing 오류

분류에서Dev

Need Google Maps to open Multiple Marker Popups with Fancybox

분류에서Dev

Why are my google maps marker positions all offset by about an inch?

분류에서Dev

Persistent title with marker in Google Maps Api v2

분류에서Dev

How to set the marker color to black in google maps android

분류에서Dev

How to add a marker in `Google Maps` view in Objective-C(iOS)

분류에서Dev

Javascript adding functions to google.maps.Map

분류에서Dev

google maps api add button to pan back to user location using geolocationmarker-compiled.js

분류에서Dev

google maps api add button to pan back to user location using geolocationmarker-compiled.js

분류에서Dev

Google Maps not working using jQuery

분류에서Dev

Does adding the Traffic layer to our Google maps invoke an additional cost?

분류에서Dev

Google Maps API v2 for Android: Marker and Polyline position mismatch

분류에서Dev

Google Maps API v3- Create a marker property that contains content from other properties?

분류에서Dev

How to create a direct link to access a specific Google Maps API V3 marker?

분류에서Dev

Google Maps Marker Shadow가 표시되지 않음

분류에서Dev

Google Maps jQuery Marker가 표시되지 않음

분류에서Dev

Google Maps Marker Clusterer : 중첩 된 클릭 처리

분류에서Dev

함수 호출시 Google Maps Circle 및 Marker 다시 그리기

분류에서Dev

How to bind google maps using HotTowel?

분류에서Dev

Add CalendarView button to Google Maps API V2 - Android

분류에서Dev

Android의 GoogleMap에서 com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker를 수정하는 방법

분류에서Dev

태그를 기반으로 Android Google Maps Marker를 제거 할 수 있습니까?

분류에서Dev

Google Maps Marker Clusterer Plus로 혼합 콘텐츠 경고를 방지하는 방법

분류에서Dev

Google Maps Marker Clusterer-위치 색인과 함께 infoWindow를 사용하는 방법

Related 관련 기사

  1. 1

    Making Google maps marker array global breaks marker click event

  2. 2

    Restrict google maps API marker to only one click per marker

  3. 3

    Google Maps Android Undo Marker Drag

  4. 4

    Parse JSON response for Google Maps Marker in Android

  5. 5

    how to get the coordinates of google maps marker and store it in textbox or label using asp.net c#

  6. 6

    @ react-google-maps Marker LatLng throwing 오류

  7. 7

    Need Google Maps to open Multiple Marker Popups with Fancybox

  8. 8

    Why are my google maps marker positions all offset by about an inch?

  9. 9

    Persistent title with marker in Google Maps Api v2

  10. 10

    How to set the marker color to black in google maps android

  11. 11

    How to add a marker in `Google Maps` view in Objective-C(iOS)

  12. 12

    Javascript adding functions to google.maps.Map

  13. 13

    google maps api add button to pan back to user location using geolocationmarker-compiled.js

  14. 14

    google maps api add button to pan back to user location using geolocationmarker-compiled.js

  15. 15

    Google Maps not working using jQuery

  16. 16

    Does adding the Traffic layer to our Google maps invoke an additional cost?

  17. 17

    Google Maps API v2 for Android: Marker and Polyline position mismatch

  18. 18

    Google Maps API v3- Create a marker property that contains content from other properties?

  19. 19

    How to create a direct link to access a specific Google Maps API V3 marker?

  20. 20

    Google Maps Marker Shadow가 표시되지 않음

  21. 21

    Google Maps jQuery Marker가 표시되지 않음

  22. 22

    Google Maps Marker Clusterer : 중첩 된 클릭 처리

  23. 23

    함수 호출시 Google Maps Circle 및 Marker 다시 그리기

  24. 24

    How to bind google maps using HotTowel?

  25. 25

    Add CalendarView button to Google Maps API V2 - Android

  26. 26

    Android의 GoogleMap에서 com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker를 수정하는 방법

  27. 27

    태그를 기반으로 Android Google Maps Marker를 제거 할 수 있습니까?

  28. 28

    Google Maps Marker Clusterer Plus로 혼합 콘텐츠 경고를 방지하는 방법

  29. 29

    Google Maps Marker Clusterer-위치 색인과 함께 infoWindow를 사용하는 방법

뜨겁다태그

보관