I'm just looking for some help for the best way to do this and if there's any recommendations on a better way. So I have a Google map on one of the web pages, I need to add multiple marks to show store locations. I am using ACF to store the data of the stores in a repeater and am using a custom post type to call the data from ACF.
Is there a better way to do this or any further recommendations?
Maybe this can help you in the right direction. I have a website on which I use a custom field where you can enter the desired address of a location ($address) to find lat/long for the given address.
$address = get_field( 'adress' );
// Convert adress to lat/long
$findaddress = urlencode($address);
$requesturl = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=true";
$xml = simplexml_load_file($requesturl) or die("not found");
$status = $xml->status;
if ($status=="OK") {
$latitude = $xml->result->geometry->location->lat;
$longitude = $xml->result->geometry->location->lng;
}
The given lat/long can than be used to add a location, marker, center etc:
// Location of the given address
var location = new google.maps.LatLng(<?php print $latitude ?>,<?php print $longitude; ?>);
// Map
var map = new google.maps.Map(document.getElementById('google-maps'), {
zoom: 18,
disableDefaultUI: true,
scrollwheel: false,
styles: styles,
center: location,
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
// Marker
var marker = new google.maps.Marker({
map: map,
position: location,
});
Related
I'm creating a new field type with the Adavanced Custom Fields, which enables the user to choose an image from the Image Library and define a point (X,Y coordinates) in this image. After defining the coordinates, the user can place an overlay video in this point. But my question is, how do I Get and Set an X and Y value in the database with the javascript API?
Okay, so #ObmerKronen suggested that I posted some code to demostrate what I'm trying to do ... makes sense:)
I've added the image in the new ACF field php file (class-tas-v5.php)
$imgID = get_field('image_url');
$img = wp_get_attachment_image( $imgID, "full", "", array( "class" => "overlay-point-img" ) );
echo '<div class="overlay-point-img-container">' . $img . '</div>';
Then I'm defining the X and Y coordinates in the input.js file
function initialize_field($field) {
var overlay_point_container = $field.context;
var img = $(overlay_point_container).find(".overlay-point-img-container");
var circle = '<div class="overlay-point-circle"></div>';
img.prepend(circle);
$(img).on("mousemove", function(event) {
var relX = event.pageX - $(this).offset().left;
var relY = event.pageY - $(this).offset().top;
$(circle).css({ left: relX, top: relY });
});
// Tried these two functions, but none of the values are saved in the database
// acf.set("posX", "123");
// $field.val("posX");
}
I've tried to save it with dummy data like this
acf.set("posX", "123");
$field.val("posX");
I am trying to iterate through GeoJSON map.data using a forEach. I want to return the position (LatLng) of each feature so I can add it to my markers array based on a feature property. Here's my attempt:
allMarkers = [];
jQuery.getJSON('json.php', function(data){
points = map.data.addGeoJson(data);
});
var eid = 30;
map.data.forEach(function(feature){
if(feature.getProperty('eid') === eid){
LatLng = feature.getGeometry().LatLng; //not sure how to get LatLng
id = feature.getProerty('id');
var marker = new google.maps.Marker({
position: LatLng,
map: map,
draggable: true,
id: id,
icon: imageActive,
});
allMarkers[id] = marker;
map.data.remove(feature);
}
});
I want to create a marker for the ones I want and remove those from the map.data and keep the remaining map.data for reference.
Any tips/suggestions are always appreciated.
It turns out that all I needed to change was "feature.getGeometry().latLng" to "feature.getGeometry().get()" like what was mentioned in the comments by #geocodezip.
I had tried "feature.get()" and that obviously didn't make sense. Not sure why I had the oversight. So what was obviously needed was:
LatLng = feature.getGeometry().get();
Thanks for the help.
I have a simple embedded map using fusion table. For some reason one of the markers disappear on the map and only shows up at certain zoom level. This is happening for newly added record.
I use fusion table query to load the map.
function initialize() {
var myoptions = {
center: new google.maps.LatLng(19.07621, 72.87766),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var locString = $.jStorage.get("loc");
map = new google.maps.Map(document.getElementById('map-canvas'),myoptions);
layerl0 = new google.maps.FusionTablesLayer({
query: {
select: "'Name'",
from: '19uKyFR_QRg0oRyE0RetwgSIUbAJtOJCxu3vd3dE',
where: "'City' = '" + locString + "'"
},
map: map
});
}
This query shld return one marker. It only appears at certain zoom level
FT servers need up to 1 minute to cache new records on all zoom levels. You might have hit this limitation.
I'm building an application using CakePHP that will store events including the event location. When a user visits the application they will see a Google Map that will get their location and show events near them in the form of little pins that they can click on to view the event details.
I have some questions though:
1.) How would I store the Location in the DB? Would the actual geolocation coordinates be the best bet and how would I make it easy for a user to create an event and enter them.
2.) Once I have the events in place how do I create custom pins with the info pulled from the DB? Example like foursquare:
3.) Whilst getting the users location using HTML5 Geolocation how do I show a little loader on the map again like Foursquare does?
So far I've managed to create the Map and make the controls minified and get the location of the viewer but I'm not sure how do 3 and show a better feedback to the user for the geolocation.
If someone could help me with those other two questions as well it'd be very much appreciated as I'm finding it very confusing so far. Thanks.
var map;
function initialize() {
var myOptions = {
zoom: 8,
panControl: false,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
myOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
1) Store the actual coordinates of the location and any extra meta data (if you have it) like place name, foursquare_id, date, etc. Storing it this way will make using the data later on straightforward, such as plotting on a map or location name lookup. This will be your Location model.
Create an Event model which you can then associate to a Location. You could hack together some nice interactive functionality using event handlers on your map markers.
Something like: "the user clicks a location on the map, up pops a box asking them would like like to create a new event at this location, marker is added to the map and a form appears where they can populate the event details, etc, etc." You get the idea.
Have a look at the Marker documentation.
2) You can set a custom image for the map markers using ImageMarker Class. Take a look at the huge set of examples for ideas of what's possible.
3) The navigator.geolocation.getCurrentPosition() method as I understand it, is asynchronous. The first argument is the successCallback.
With this in mind, you could set an overlay on your map: "Finding your location", then make the call to getCurrentPosition(). In your successCallback function, you would then hide the overlay.
I wonder whether someone may be able to help me please.
I've put some coding together (please see below) whereby a user goes onto a HTML form, they type in an address and click a 'Search' button. Upon doing this, the location is plotted on the Google map and the Lat and Long co-oridnates are automatically entered into the associated text boxes on my form.
What I would like to do, if at all possible, is for the marker to be draggable so the user can fine tune the location, and as they drag the marker, I'd like for the Lat and Long fields to change their
associated co-ordinates.
In addition, I'd also like, if at all possible, to have a field on the form called 'NearestAddress' to show the nearest address to where the marker has been dragged to.
I've managed to make the markers draggable but they don't update the Latitude and Longitude text boxes. I'm also unsure how to add the functionality to show the updated address to where the marker has been dragged to.
(function() {
// Defining some global variables
var map, geocoder, myMarker, infowindow;
window.onload = function() {
// Creating a new map
var options = {
zoom: 3,
center: new google.maps.LatLng(55.378051,-3.435973),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), options);
// Getting a reference to the HTML form
var form = document.getElementById('LocationSearchForm');
// Catching the forms submit event
form.onsubmit = function() {
// Getting the address from the text input
var address = document.getElementById('Address').value;
// Making the Geocoder call
getCoordinates(address);
// Preventing the form from doing a page submit
return false;
}
}
// Create a function the will return the coordinates for the address
function getCoordinates(address) {
// Check to see if we already have a geocoded object. If not we create one
if(!geocoder) {
geocoder = new google.maps.Geocoder();
}
// Creating a GeocoderRequest object
var geocoderRequest = {
address: address
}
// Making the Geocode request
geocoder.geocode(geocoderRequest, function(results, status) {
// Check if status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
// Center the map on the returned location
map.setCenter(results[0].geometry.location);
// Creating a new marker and adding it to the map
var myMarker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
draggable:true
});
document.getElementById('Latitude').value= results[0].geometry.location.lat();
document.getElementById('Longitude').value= results[0].geometry.location.lng();
google.maps.event.addListener(myMarker, 'dragend', function(evt){
document.getElementById('current').innerHTML = '<p>Marker dropped: Current Lat: ' + evt.latLng.lat().toFixed(3) + ' Current Lng: ' + evt.latLng.lng().toFixed(3) + '</p>';
});
google.maps.event.addListener(myMarker, 'dragstart', function(evt){
document.getElementById('current').innerHTML = '<p>Currently dragging marker...</p>';
});
map.setCenter(myMarker.position);
myMarker.setMap(map);
}
});
}
})();
I am new to Google maps development and I'm not even sure whether it's possible to achieve what I want. I've been working on this now for a few weeks and it's driving me a little crazy, so if someone could perhaps point me in the right direction it would gratefully be received.
Many thanks and kind regards
Chris
Instead of evt.latLng.lat().toFixed(3) you should just use the myMarker object and grab it's position.
Getting the nearest address is not that easy, but requires reverse geocoding, and to be honest I don't see the point in doing it. You would have to make special cases for the occurences where there couldn't be found a closest address and stuff like that.
If you really want to do it though there is a webservice you can call to do it.