I'm using google maps API and I need to save the position of each user after they select it by dragging the marker, knowing that they will be changing it from their profile and not an update page.
So basically I need to save the lat and lng of the user in the table right after they move marker, I haven't found a proper solution since we usually have regular forms with submit buttons to update. So here's the google maps div from the twig
<div id="map-canvas" style="width: 500px; height: 380px"></div>
<script src="https://maps.googleapis.com/maps/api/js?key="
type="text/javascript"></script>
<script>
var map = new google.maps.Map(document.getElementById('map-canvas'),{
center:{
lat: 45,
lng: 45
},
zoom:15
});
var marker = new google.maps.Marker({
position:{
lat: 45,
lng: 45
},
map: map,
draggable: true
});
google.maps.event.addListener(marker,'dragend',function () {
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();
$('#lat').val(lat);
$('#lng').val(lng);
})
</script>
and here's the controller for the profil page
public function showEtabbAction(){
$em = $this->getDoctrine()->getManager();
$etab = $this->getUser()->getEtablissement();
$userNom = $this->getUser()->getNom();
return $this->render('#AllforkidsUser/Etablissement/etab.html.twig',array("etablissement"=>$etab,"nom"=>$userNom));
}
and thanks
Put two hidden fields in your form and then on dragend map event, set the value of this hidden fields from the marker position. Then you can handle the request as a regular form.
Related
I have a component with a section tag with an id and ref of map. The id is used to load a Google Map, don't worry about this.
<section
id="map"
ref="map"
>
</section>
I have a function called showUserLocationOnTheMap() with a button called Map that's invoked when a User clicks on it. This function shows a marker on the map based on an address, that is stored in the db from a previous step.
showUserLocationOnTheMap(latitude, longitude) {
let map = new google.maps.Map(document.getElementById("map"), {
zoom:15,
center: new google.maps.LatLng(latitude, longitude),
mapTypeId:google.maps.MapTypeId.ROADMAP
});
const marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
map: map
});
google.maps.event.addListener(marker, "click", () => {
const infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(
`<div class="ui header">
<h6>company name</h6>
</div>
`
);
infoWindow.open(map, marker);
});
this.$refs.map.scrollToTop();
},
<button
#click="showUserLocationOnTheMap(item.profile.latitude,item.profile.longitude)"
class="apply-job-btn btn btn-radius btn-primary apply-it"
>
Map
</button>
What I'm trying to do is, I have a bunch of search results, so when someone is scrolling far down the page and they click the map button from the record, it will do two things.
It shows a marker on the map (This is working)
It scrolls back up to the top of the page so the User can see the Map with the marker on it.
WHAT I TRIED:
I created this function below:
scrollToTop() {
this.$el.scrollTop = 0;
}
I added a ref attribute called map to my Section tag and then at the end of my showUserLocationOnTheMap() function I called this line (as you can also see above).
this.$refs.map.scrollToTop();
The problem is I'm getting this error:
Cannot read property 'scrollToTop' of undefined"
I have a GmapMap component in my Vue site and would like to display the traffic on the map with a traffic layer that can be toggled on or off.
The following gives me the terrain layer activated, but I don't see the traffic layer.
<GmapMap ref="baseMap" map-type-id="terrain" >
...
</GmapMap>
This is how you implement it in the normal non Vue way:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {lat: 34.04924594193164, lng: -118.24104309082031}
});
var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
}
Thanks to #Antonio I got this to work. It now displays the traffic all the time, so if anybody has a way to toggle the layer please let me know.
mounted() {
this.$refs.baseMap.$mapPromise.then(map => {
const trafficLayer = new google.maps.TrafficLayer()
trafficLayer.setMap(map)
})
}
I have a Google map driven by Advanced Custom Fields. I have created an iconType to dictate which icon is used as the marker depending on the custom field.
My marker images are 80px square but I want to force them to 40px square for retina reasons. I have tried lots of variations of code I've found via google etc but nothing works and the size gets ignored showing my markers at the larger 80px. My guess is because I have the 2 icons depending on iconType?
Can anyone help me force the size of these markers to 40px square please...
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng'));
var iconType = $marker.attr('data-icon');
var iconBase = '<?php echo get_template_directory_uri(); ?>/assets/';
var size = new google.maps.Size(40, 40);
if (iconType === 'Costa') {
iconName = 'costa#2x.png';
} else {
iconName = 'starbucks#2x.png';
}
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map,
clickable: false,
animation: google.maps.Animation.DROP,
icon: iconBase + iconName,
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
}
}
I did something like this recently, and I believe you'll need to use the new Icon object to specify your custom Icon properties, and use the size and scaledSize properties. I fixed my issues following this guide:
How to add retina ready pin marker to your Google maps?
i have a probleme to my infowindow, i can't show my title and description on it.
The relation between my data base and google api is good because i can show my marker on the map but when i click on it there is nothing in the infowindow.
Here's my code:
function createMarker(lat, lng, titre, description, adresse){var latlng = new google.maps.LatLng(lat, lng);var marker = new google.maps.Marker({position: latlng,map: map,title: titre});
contentString =
'<div class="descritpion" >'+'<a>(titre)</a>'+''
'</div>';
var infobulle = new google.maps.InfoWindow({content: contentString,});google.maps.event.addListener(marker, 'click', function(){infobulle.open(map, marker);});}
This is sort of how I do it (though I usually also hold all the markers in an array so they can be cleared, or updated as a group). Also, I'm assuming your syntax around titre was wrong, and have corrected to to be what I thought you wanted to achieve.
var MyInfoWindow = new google.maps.InfoWindow({content: 'Loading...'});
function createMarker(lat, lng, titre, description, adresse){
var point=new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({position: point,map: map});
marker.html='<div class="descritpion" ><a>('+titre+')</a></div>';
google.maps.event.addListener(marker, 'click', function () {
MyInfoWindow.setContent(this.html);
MyInfoWindow.open(map, this);
});
}
I am working a Yelp-like project that associates reviews to places. Yet, we don't have any database of places and we'd like to leverage a third-party api.
We'd like to start with a auto-complete search box like on Google Maps. For example, the user enters "tamar" and it suggests "Tamarine Restaurant, Palo Alto, CA, United States".
1) is it possible? what third party api would you use? Is Google maps the best?
Once the results are returned, the user selects one place and will leave a review.
2) how would you associate that review to that specific place in the DB? My concern is if later on we change api provider, that we can still associate our reviews to the new api places.
Thank you SO MUCH! Any suggestion would help.
Fab
this will generate autocomplete also get the latitutde and longitutde in 2 textbox for storage on your DB if need.initially center is set statically ..hope this is what you want
<head>
<script type="text/javascript" src=" https://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
<script type="text/javascript">
var map;
var geocoder;
geocoder = new google.maps.Geocoder();
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var input = document.getElementById('searchTextField');
var options = {
bounds: pyrmont,
//types: ['establishment'] dont mention it we need to get both bussiness andaddress
};
autocomplete = new google.maps.places.Autocomplete(input, options);
}
$(document).ready(function() {
$("#find").click(function(){
var address = document.getElementById("searchTextField").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var test = results[0].geometry.location;
var latlang=String(test);
latlang = latlang.substring(0, latlang.length-1);
latlang= latlang.substr(1);
var latlan = latlang.split(",");
var lat = latlan[0];
var lon = latlan[1];
$("#latitude").val(lat);
$("#longitude").val(lon);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
});
</script>
<body onload="initialize();">
<label> Type address</label>
<input type="text" id="searchTextField" style="width:500px;" />
<div id="map" style="width:500px;height:500px;"></div>
<input type="text" id="latitude"/>
<input type="text" id="longitude"/>
<input type="button" id="find" value="Find"/>
</body>
There's an API at http://compass.webservius.com that can help. You can search a database of millions of businesses in the US (e.g. by a name prefix like in your example, by zip code, by latitude/longitude bounding box, etc) and get back results (such as business name, address, type, etc) in XML or JSON format.
For the second part of your question, it is tough because there is no "universal ID" for a business, but usually a combination of normalized postal address + business name will work well enough.
html
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
HTML Form Input
<input id="locationName" name="locationName" type="text">
JavaScript
function init() {
var input = document.getElementById('locationName');
var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', init);