2013-01-08 210 views
0

我想提供一個功能,如用戶將提供一個城市,州和國家。然後我的目標是標記用戶在谷歌地圖上提供的位置。按位置標記位置谷歌地圖v3

我的代碼是:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)"> 
    <meta name="dcterms.created" content="Tue, 08 Jan 2013 05:31:17 GMT"> 
    <meta name="description" content=""> 
    <meta name="keywords" content=""> 
    <title></title> 
    <?php 
// get and breakdown the results then store them in $var's 
$Address = "99999 parkplace, new york, NY"; 
$urladdress = urlencode($Address); 
$Base_url = "http://maps.google.com/maps/geo?q="; 
$urlParts = "&output=xml"; 
$urlrequest = $Base_url . $urladdress . $urlParts; 
$xml = simplexml_load_file($urlrequest); 
$num = "0"; 
$value=$xml->Response->Placemark; 
    $GeoFindAdd{$num} = $value->address; 
    $GeoFindCords{$num} = $value->Point->coordinates; 
?> 
    <script 
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> 
</script> 
    <script> 
     var myCenter=new google.maps.LatLng(<?php echo $GeoFindCords{$num}; ?>); 

function initialize() 
{ 
var mapProp = { 
    center:myCenter, 
    zoom:5, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); 

var marker=new google.maps.Marker({ 
    position:myCenter, 
    }); 

marker.setMap(map); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
<div id="googleMap" style="width:500px;height:380px;"></div> 
    </body> 
</html> 

代碼的輸出是

http://wfs-01.wapka.mobi/300030/300030787_6850a0ae9a.png

有什麼問題我在做什麼?或者通過提供位置來標記位置的任何其他方式。

謝謝。

回答

2

我得到了答案

<!DOCTYPE html> 

<html lang="en"> 

<head> 

<meta charset="utf-8"> 

<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)"> 

<meta name="dcterms.created" content="Tue, 08 Jan 2013 05:31:17 GMT"> 
    <meta name="description" content=""> 

<meta name="keywords" content=""> 

<title></title> 

<script 
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"> 
</script> 

<script> 

var geocoder, map; 

function initialize() { 

geocoder = new google.maps.Geocoder(); 

var latlng = new google.maps.LatLng(10, 10); 

var myOptions = { 
    zoom: 0, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 

map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

codeAddress(); 

} 


function codeAddress() { 

var address = "Kottayam, Kerala"; 

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 
     }); 

} 
else { 

alert("Geocode was not successful for the following reason: " + status); 
    } 
    }); 
} 

</script> 

</head> 

<body onload="initialize()"> 
<div id="map_canvas" style="width: 320px; height: 480px;"></div> 

</body> 
</html> 
相關問題