Google Maps user location

妖精的绣舞 提交于 2019-12-13 05:15:39

问题


I wanna set up a way in which users can place a marker on the map to tell their address and i should be able to extract the lat and long and store it in the database.

Is there somekinda plugin which i can modify and use ?


回答1:


First create a function that a accepts a latlng object. This function will add the info to your database and then add the marker if it was successful.

function addRestaurant( latlng ) {
    lat = latlng.lat;
    lng = latlng.lng;
    //Code to add restaurant
    if ( dbase_successful ) {
        var marker = new google.maps.Marker({
         position: latlng,
         title: "Some text"
            map: map //make your map global
        });
    }
}

Then add an event listener on the click event of the map that calls the function you just created. Add this to your map initialize code.

google.maps.event.addListener(map, 'click', function(event) { addRestaurant( event.latlng ) } );

Now when your map is clicked add_restaurant will be called with the latlng of the click event on the map.




回答2:


Maybe this is what you are looking for:

Im using javascript for this.

here you set a marker from the address.

var address= "denmark Århus";
  geocoder.getLatLng(
                  address,
                  function(point) {
                    if (!point) {
                      alert(address + " not found");
                    } else {
                      map.setCenter(point, 14);
                      map.addOverlay(new GMarker(point, markerOptions));
                      }
                    }
                  ); 


来源:https://stackoverflow.com/questions/4042531/google-maps-user-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!