Delete/move Google maps marker (jquery-ui-maps) - javascript

I want to remove one marker from my Google Map, but I can't seem to get it to work. I find various answers, all telling me to use .setMap(null) on the marker, but I can't seem to get it to work.
$map_canvas = $('#map_canvas');
var youreHere_Marker;
function centerMapToAddress( address ) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if( typeof youreHere_Marker!=="undefined"){
youreHere_Marker.setMap(null);
}
youreHere_Marker = $map_canvas.gmap('addMarker', {'position': results[0].geometry.location.lat()+','+results[0].geometry.location.lng(), 'bounds': true});
}
});
}
I get TypeError: youreHere_Marker.setMap is not a function. To my knowledge this means that the variable youreHere_Marker doesn't have the method .setMap(), but if I do console.log(youreHere_Marker) and inspect the object, I can see the method.
I have more markers on my map, via the MarkerClusterer. Those should remain untouched
I have the feeling I'm close, could someone point me in the right direction?
Edit: I've also tried .setPosition(), same error. I'm assuming I'm using the variable incorrect, but I don't know how to refer to it properly.

Well, i was working with google maps without jQuery, but i think (i'm not sure, but you may try) that you should get your marker with the following code:
youreHere_Marker = $map_canvas.gmap('get', 'markers')[0];
youreHere_Marker.setMap(null);
I'm really not sure that it will do what you want, but there is a possibility that this will work : )
I hope you'll solve you problems.
Thanks. : )

Related

Google maps api, gecode. Trying to geocode a language specific address

I'm sure this question has been asked somewhere, but for the life of me i can't seem to find it..
I have an address in "København S" denmark, that i am trying to geocode so i can place a marker on my map. However i get the zero results error, and i can see that
"København S"
has been changed to
"København S"
with the hex.
I'm sure this is fairly easy to get around, but i have been trying the past hour and i think im making it to complicated for myself. Is there an easy workaround to specify the character set i want it to use rather than it being in partly hex?
Here is my code, the error msg and the msg with the hex.
function geocodeAddress() {
var geocoder = new google.maps.Geocoder();
if (geoIndex < addressesToGeo.length) {
var addressSplit = addressesToGeo[geoIndex].split("SPLIT"); // temp
alert(addressSplit[0]); // second alert msg image
geocoder.geocode({ 'address': addressSplit[0] }, makeCallBack(parseInt(addressSplit[1])));
++geoIndex;
}
else {
clearInterval(geoTimer);
}
}

Zoom is too great in google maps api

Here is my fiddle . Here i have displayed current location and end location by balloons.
And i am trying to put directions between start and end point in the map. In this case no markers for the directions are displayed may be because both shares common location. But the zoom is too great , results in it is not covering start and end point. User have to make double right click to see both start and location.
I have also tried,
new_boundary = new google.maps.LatLngBounds();
new_boundary.extend(new google.maps.LatLng(start));
new_boundary.extend(new google.maps.LatLng(end));
map.fitBounds(new_boundary);
But it is not working. Whats wrong with my map configuration ?
your script breaks (at least for me) at this line:
dir=((Math.atan2(z.lng()-a.lng(),z.lat()-a.lat())*180)/Math.PI)+360
take a look at this line:
z=(step.lat_lngs.length)?step.lat_lngs[1]:step.end_point,
for the first step lat_lngs.length is 1 , so step.lat_lngs[1] is undefined, the call of z.lng() fails with "Cannot call method 'lng' of undefined"
Possible fix for that error:
z=(step.lat_lngs.length>1)?step.lat_lngs[step.lat_lngs.length-1]:step.end_point,
Related to the zoom:
When you wouldn't have disabled the complete UI you would have seen that the result (zoom ) is correct.
The DirectionsRenderer by default will refresh the viewport of the map so that the complete route is visible.
This will be done(the bounds initally set in your script will be discarded).
To have another result(preserve the current viewport, but extend it that also the route is visible), you must:
set the preserveViewPort-option of the DirectionsRenderer to true(default is false)
extend the bounds of the map with the bounds of the route
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.set('preserveViewport',true);
map.fitBounds(map.getBounds().union(response.routes[0].bounds));
//continue with your code
This code is correct for example
Your map change resolution when you add
directionsDisplay.setDirections(response);
setDirections(directions:DirectionsResult) None Set the renderer to
use the result from the DirectionsService. Setting a valid set of
directions in this manner will display the directions on the
renderer's designated map and panel.
Hope, that i understand problem right, English i not my native language
Try to add validation as
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
if(!response.routes[0].bounds.getNorthEast().equals(response.routes[0].bounds.getSouthWest())){
directionsDisplay.setDirections(response);
}
addDirections(response.routes[0]);
}
});
To check if answer contains different point

Google Maps Geocode: unknow property premise

I'm trying to add a marker on a building/premise on google maps, to do this I'm using the Geocoding API. I tested it out by requesting an address and it worked perfectly.
var address = "...";
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location_marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
According to the Geocoding API you can request a building/premise by using the premise property, but when using premise I'm getting a JavaScript error Uncaught InvalidValueError: unknown property premise.
This is how I'm requesting the premise property:
var premise = "...";
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'premise': premise }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location_marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
I followed the answer given for this question
Not sure if anyone has had this problem before. Maybe there's another way to tackle this as well.
UPDATE
In case someone stumbles upon this question, to search businesses/places on Google Maps you can use the Places Library. This returns objects with the business's address, location, name etc.
Hopefully this helps someone :)
I there's some confusion here about what the Geocode API does. Read the API again for requests.
The required parameters are address or latlng or components, and sensor.
The optional parameters are bounds, language, region and components.
Premise is one of the properties of the returned JSON data. This means that you can't use it as a search parameter which is why the API is complaining.
The answer in that question to which you linked is wrong.
Hope this is helpful.

Javascript skipping lines when trying to geocode location

I'm trying to use this function to geocode a string passed to it into a google maps result.
function codeAddress(address) {
var firstResult;
geocoder.geocode( { 'address': address}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
firstResult = results[0];
} else {
firstResult = "failed";
}
});
return firstResult;
}
The problem is that when I try to debug it using the debugger from chrome and insert a breakpoint outside and inside the ceocoder.geocode statement, I can clearly see the program execution go at the third line, but it skips the inner lines and goes straight to the return value (returning an undefined value). Some other times, it goes through the if statement within it, but it doesn't go to the return statement although I have set up a breakpoint there.
Am I trying to do this the wrong way? How can I fix this?
I found some possible answers on StackOverflow, they might help:
Aleem Saadullah on SO posted:
Finally figured it out. It was a silly error. I had linked my Javascript file before linking the jQuery. The code works fine now.
Mikko Ohtamaa answered on SO:
To be strict, whatever custom CGI you are using does not conform
JavaScript syntax.
What I suggest is that you make your custom dynamic processing into
JavaScript comments, so that it doesn't affect the normal JavaScript
parsing. This is much easier than writing your custom JavaScript
parser to cater your custom syntax.
E.g.
// %import and other custom commands here
The best approach is that you would not put any non-JavaScript to JS
files at all. If you need importing and such there are some more
generic JavaScript solutions for them.
http://browserify.org/
http://requirejs.org/
EDIT 2:
Found an answer on Engineer's answer on SO:
geocoder.geocode works asynchronously, so you need to wait until its
response will be delivered from google's servers, and only then use
responded data. Put your loop inside of callback:
geocoder.geocode( { 'address': zip }, function(results, status) { // status is empty
if (status == google.maps.GeocoderStatus.OK) {
var userLat = results[0].geometry.location.lat();
var userLng = results[0].geometry.location.lng();
userLatLng = results[0].geometry.location;
for (var i = data.length-1; i--;) {
//loop body
}
}});//end geocode

Google map api & tparkin point-in-poly

I have been working on this site for about a week now. It isnt great but then again I am not great the web design. Any tips on improvements would be greatly appreciated.
Moving on I am having a problem with point in polygon extension that tparkin wrote for the google maps api. I have read through the read me, other post and everything but I can figure out why I keep getting this error ...
a.lat is not a function
[Break On This Error] Ba(I,function(a){return!a?k:this.Z[xb]...{return new Q(this.Z.d,this.aa.d,i)};
I think it might be a problem with me using the functionality of tparkins extension incorrectly but like I said I cant figure out it.
Any help is greatly appreciated in advance.
This is the website with the script problem. Please let me know if you need any additional information.
my website.
I figured out the problem I was having. Check it out.
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var isWithinPolygon = polygon85.containsLatLng(results[0].geometry.location);
alert(isWithinPolygon);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
So the issues before was that instead of passing the coordinates results[0].geometry.location, I was passing it just location. Its a simple case of me not fully understanding what I was working with. Thank you for everyone that looked into this and sorry I wasted your time.
--Zac

Categories

Resources