2012-01-28 95 views
0

在我的iOS應用程序我想顯示的是由谷歌提供的交通信息,使用谷歌地圖API V3,但加載谷歌地圖API V3地圖在UIWebView的原因地圖不是由MKMapView..So IM內存泄漏。特別是當我們縮放地圖並單擊衛星按鈕時。 碼 -谷歌地圖API V3和UIWebView的iOS的內存問題

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
    html { height: 100% } 
    body { height: 100%; margin: 0; padding: 0 } 
    #map_canvas { height: 100%;} 
    #route_table { height: 0%;} 
</style> 
<script type="text/javascript" 
    src="http://maps.googleapis.com/maps/api/js?sensor=true"> 
</script> 
<script type="text/javascript"> 

    var directionsDisplay = new google.maps.DirectionsRenderer(); 
    var directionsService = new google.maps.DirectionsService(); 
    var arrayInput = []; 
    var trafficLayer; 
    var map; 
    var markers = []; 
    var bounds; 
    var zoomWidth; 
    var alertBOOL; 

    function initialize() { 
    var txt = new String(%@); 
    arrayInput = txt.split(','); 
    //var latlng = new google.maps.LatLng(parseFloat(arrayInput[0]),parseFloat(arrayInput[1])); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
     zoom: 5, 
     center: latlng, 
     disableDefaultUI:true, 
     streetViewControl:false, 
     backgroundColor: '#FFFFF', 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    this.map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
    directionsDisplay.setMap(this.map); 
    directionsDisplay.suppressInfoWindows = true; 
    addTrafficButton(); 
    addSatelliteButton(); 
    calcRoute(arrayInput); 
    } 

    function calcRoute(inputArray) { 
    var i=0; 
    var wps = []; 
    var start; 
    var end; 
    var i = 0; 
    var j=0; 
    for(i=0;i<arrayInput.length-1;i=i+2)  
    {  

     if(i==0) 
     {  
     start = new google.maps.LatLng(parseFloat(arrayInput[i]),arrayInput[i+1]); 
     this.map.center = start; 
     } 
     else if(i==(arrayInput.length-2)) 
     { 
     end = new google.maps.LatLng(parseFloat(arrayInput[i]),arrayInput[i+1]); 
     } 
     else 
     {  
     wps[j] = { location: new google.maps.LatLng(parseFloat(arrayInput[i]),arrayInput[i+1]) }; 
     j++; 
     } 

    } 
    bounds = new google.maps.LatLngBounds(start,end); 
    this.map.fitBounds(bounds); 
    var request = 
    { 
     origin:start, 
     destination:end, 
     waypoints: wps, 
     travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 
    directionsService.route(request, function(result, status) 
    {  
     if (status == google.maps.DirectionsStatus.OK) 
     { 
     directionsDisplay.setDirections(result); 
     } 
    }); 
} 
function addTrafficButton() 
{ 
    var tbutton = document.createElement("button"); 
    tbutton.innerHTML = "Traffic On"; 
    tbutton.style.position = "absolute"; 
    tbutton.style.bottom = "50px"; 
    tbutton.style.right = "15px"; 
    tbutton.style.zIndex = 10; 
    tbutton.style.width = "70px"; 
    tbutton.style.height = "30px"; 
    this.map.getDiv().appendChild(tbutton); 
    tbutton.className = "lolight"; 
    tbutton.onclick = function() { 
    if (tbutton.className == "hilight") { 
     tbutton.innerHTML = "Traffic On"; 
     this.trafficLayer.setMap(null); 
     this.trafficLayer = null; 
     tbutton.className = "lolight"; 
    } else { 
     tbutton.innerHTML = "Traffic Off"; 
     this.trafficLayer = new google.maps.TrafficLayer(); 
     this.trafficLayer.setMap(this.map); 
     tbutton.className = "hilight"; 
    } 
    } 
} 

function addSatelliteButton() 
{ 
    var sbutton = document.createElement("button"); 
    sbutton.innerHTML = "Satellite"; 
    sbutton.style.position = "absolute"; 
    sbutton.style.bottom = "90px"; 
    sbutton.style.right = "15px"; 
    sbutton.style.zIndex = 10; 
    sbutton.style.width = "70px"; 
    sbutton.style.height = "30px"; 
    this.map.getDiv().appendChild(sbutton); 
    sbutton.className = "lolight"; 
    sbutton.onclick = function() { 
    if (sbutton.className == "hilight") { 
     sbutton.innerHTML = "Satellite"; 
     map.setMapTypeId(google.maps.MapTypeId.ROADMAP);  
     sbutton.className = "lolight"; 
    } else { 
     sbutton.innerHTML = "Map"; 
     sbutton.className = "hilight"; 
     map.setMapTypeId(google.maps.MapTypeId.HYBRID); 
    } 
    } 
} 

</script> 
</head> 
<body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:100%;"></div> 
</body> 
</html> 

回答

0

我覺得你的問題可以用ARC到iOS 5中得到解決。它是最好的功能,通過蘋果加入到IOS 5,它擁有整個存儲管理到您的應用程序。去與ARC它幫助你很多。

+0

我的應用程序是爲iOS 4.x和iOS 5的兩種.. – iosfanboy9 2012-01-28 06:59:20

+0

還是你有問題到您的應用程序..這樣你就可以告訴你,在這個時候面臨什麼樣的問題,所以我可以爲它提供妥善的解決方案。 – Anjan 2012-01-28 07:21:54

+0

UIwebView內存從2 mb增加到15+ mb,同時縮放... – iosfanboy9 2012-01-28 07:42:51