0

我使用 Titanium Appcelerator 制作了一个基于地图的应用程序。我在地图上显示用户的当前位置。我还有一个文本框,我在其中获取用户所需的位置,以便在地图上显示它。为了在地图上显示特定位置,我使用了注释并且工作正常。问题是,当文本框的值发生变化时,会在地图上创建另一个注释。我想要做的是,我想在放置新注释之前清除以前的注释。我的 app.js 文件如下所示:

应用程序.js:

//create the window
var win1 = Titanium.UI.createWindow({
title:'Exercise Tracker',
backgroundColor: '#000'
});
//goto location label
var location_label = Titanium.UI.createLabel({
left:'10',
top:'20',
text:'Go to: ',
font:{fontSize:20}
})
//goto location textbox
var location_textbox= Titanium.UI.createTextField({
top: '15',
left: '85',
width: '300',
height: '50',

})
//go button
var btnGo=Ti.UI.createButton(
    
    {           
        top:'15',
        left:'420',
        width:'50',
        height:'50',
        title:"Go"
    })


//create our mapview
var mapview = Titanium.Map.createView({
top: 110,
height: 'auto',
width: 'auto',
mapType: Titanium.Map.STANDARD_TYPE,
region: {latitude: 51.50015,
longitude:-0.12623,
latitudeDelta:0.5,
longitudeDelta:0.5},
animate: true,
regionFit: true,
userLocation: true,

});
// to get values from both the textbox      
btnGo.addEventListener('click', function (e){
//var addr=location_textbox.value;

var addr="";
addr = location_textbox.value;
annote(addr);
});

// to refrsh the page on click textbox  
location_textbox.addEventListener('click', function (e){

});

//--------------------------annotations--------------------------------
//var addr = 'Howrah';
function annote(addr)
{
var addr_fw=addr; 
Titanium.Geolocation.forwardGeocoder(addr,function(evt) {
var objLocationAnnotation = Titanium.Map.createAnnotation({
    latitude: evt.latitude,
    longitude: evt.longitude,
    title: addr_fw
});
mapview.addAnnotation(objLocationAnnotation);
});
} 
//--------------------------annotations--------------------------------
//add the map to the window
win1.add(mapview);
//set the distance filter
Titanium.Geolocation.distanceFilter = 10;


Titanium.Geolocation.getCurrentPosition(function(e)
{
if (e.error)
{
//if mapping location doesn't work, show an alert
alert('Sorry, but it seems geo location is not available on your device!');
return;
}
//get the properties from Titanium.GeoLocation
var longitude = e.coords.longitude;
var latitude = e.coords.latitude;
var altitude = e.coords.altitude;
var heading = e.coords.heading;
var accuracy = e.coords.accuracy;
var speed = e.coords.speed;
var timestamp = e.coords.timestamp;
var altitudeAccuracy = e.coords.altitudeAccuracy;
//apply the lat and lon properties to our mapview
mapview.region = {latitude: latitude,
longitude: longitude,
latitudeDelta:0.5,
longitudeDelta:0.5
};
});
//finally, open the window
win1.open();
//adding display properties to the main window      
win1.add(location_label);
win1.add(location_textbox);
win1.add(btnGo); 
4

2 回答 2

3

使用 mapView.removeAnnotation String/Titanium.Map.Annotation annotation

它从地图中删除现有注释。

从此处阅读文档以获取更多详细信息。

http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Map.View-method-removeAnnotation

于 2012-11-30T13:09:07.453 回答
0

改变

mapview.addAnnotation(objLocationAnnotation);

mapview.annotations = [objLocationAnnotation];

这将清除地图视图上的所有注释并将它们设置为一个新注释的新数组。

于 2012-08-29T11:38:26.347 回答