PhoneGap / Cordova:谷歌地图API - 我的位置

时间:2012-08-01 06:27:27

标签: javascript google-maps cordova google-maps-api-3

我可以使用PhoneGap中的Google Maps API在地图上显示我的位置。现在它只是显示地图 - 没有pindrop或类似的东西。我想知道是否有人知道如何在你提出的位置放一个引脚?

我到目前为止的代码是:

//GEOLOCATION
            var onSuccess = function(position) {
                var myLat = position.coords.latitude;
                var myLong = position.coords.longitude;

                //MAP
                var mapOptions = {
                    center: new google.maps.LatLng(myLat, myLong),
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

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

            };

            // onError Callback receives a PositionError object
            //
            function onError(error) {
                alert('code: '    + error.code    + '\n' +
                      'message: ' + error.message + '\n');
            }

            navigator.geolocation.getCurrentPosition(onSuccess, onError);

如果有人能告诉我如何在该位置放置一个非常棒的针脚!感谢

1 个答案:

答案 0 :(得分:4)

发现它! https://developers.google.com/maps/documentation/javascript/overlays#Markers

调整了我的代码:

//GEOLOCATION
            var onSuccess = function(position) {
                var myLat = position.coords.latitude;
                var myLong = position.coords.longitude;

                //MAP
                var mapOptions = {
                    center: new google.maps.LatLng(myLat, myLong),
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

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

                var marker = new google.maps.Marker({
                                                    position: new google.maps.LatLng(myLat, myLong),
                                                    map: map,
                                                    title:"Hello World!"
                                                    });

            };

            // onError Callback receives a PositionError object
            //
            function onError(error) {
                alert('code: '    + error.code    + '\n' +
                      'message: ' + error.message + '\n');
            }

            navigator.geolocation.getCurrentPosition(onSuccess, onError);