Main image
2nd April
2011
written by admin

Draggable Marker Map with Streetview

Here is a free code snippet that will create this map here on your website easily.


Grab the Code:

Before you copy the code we have added some BOLDED text where you have to change some options to fix the css and html bits up. It gets complicated when trying to just copy and paste various code into text. ;-)

Also in BOLD is some options on the map, and where you have to create the css.

[script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">[/script>
replace [ with <
var geocoder = new google.maps.Geocoder();
var marker;
var latLng;
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}

function updateMarkerStatus(str) {
document.getElementById('markerstatus').innerHTML = "";
}

function updateMarkerPosition(latLng) {
document.getElementById('lat').value =
latLng.lat();
document.getElementById('lng').value = latLng.lng();

}

function code(){

// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);

// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});

google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});

google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});

}
function updateMarkerAddress(str) {
document.getElementById('address').value = str;
}

function initialize() {
latLng = new google.maps.LatLng(53.54418008858284, -2.0549746093749945);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 6,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
These coordinate represent the starting location for the pin
marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});

code()
}

// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);

function str() {
var lat = document.getElementById('lat').value
var lng = document.getElementById('lng').value
latLng = new google.maps.LatLng(lat,
lng);

var panoramaOptions = {
position:latLng,
pov: {
heading: 270,
pitch:0,
zoom:1
}
};

var myPano = new google.maps.StreetViewPanorama(document.getElementById("mapCanvas"), panoramaOptions);
myPano.setVisible(true);
marker = new google.maps.Marker({
position: latLng,
map: myPano,
draggable: true
});

code()
}
function backtomap(){
var lat = document.getElementById('lat').value
var lng = document.getElementById('lng').value
latLng = new google.maps.LatLng(lat,
lng);

var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});

code()
}
[ /script> replace [ with <

CREATE table or CSS to place buttons in place and add id functions

[ form> replace [ with <
create button style " onclick="str()">Street View close css
create button style;" onclick="backtomap()">Road Mapclose css

example style :button style="width:100px;

Marker Position :
[ input id="lat" name="lat" size="18" type="text" /> replace [ with <
[ input id="lng" name="lng" size="18" type="text" /> replace [ with <

Current Address:
[ input id="address" size="41" type="text" /> replace [ with <

[ /form> replace [ with <

Use the Comments

We know these code snippets might not suit all needs, but if you are unsure of anything just drop us a line, and we can always help you if you are stuck. If you want to see changes to the map, drop us a line too. We may already have what you need.

1 Comment

  1. 03/04/2011

    The source code above might give you a bit of hassle. No worries. I have added the full source code on a seperate html page here.

Leave a Reply