我在做一個地圖應用程序[閱讀KML文件並將其顯示到Google地圖中],並且我正在使用本網站中討論過的類。這堂課如下圖所示。閱讀KML文件並將其顯示到Google地圖中
我面臨一些問題 1-的R.layout.main >>有在主
2-也錯誤,則DEST [1]和dest [0]它們不識別
3-也drowpath功能沒有被識別
我得到這個類從這個鏈接How to draw a path on a map using kml file?
package com.test.map;
import com.test.map.R ;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class DirectionMapActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");//from
urlString.append(Double.toString(lastKnownLocation.getLatitude()));
urlString.append(",");
urlString.append(Double.toString(lastKnownLocation.getLongitude()));
urlString.append("&daddr=");//to
urlString.append(Double.toString((double)dest[0]/1.0E6));
urlString.append(",");
urlString.append(Double.toString((double)dest[1]/1.0E6));
urlString.append("&ie=UTF8&0&om=0&output=kml");
try{
// setup the url
URL url = new URL(urlString.toString());
// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
// create the reader (scanner)
XMLReader xmlreader = parser.getXMLReader();
// instantiate our handler
NavigationSaxHandler navSaxHandler = new NavigationSaxHandler();
// assign our handler
xmlreader.setContentHandler(navSaxHandler);
// get our data via the url class
InputSource is = new InputSource(url.openStream());
// perform the synchronous parse
xmlreader.parse(is);
// get the results - should be a fully populated RSSFeed instance, or null on error
NavigationDataSet ds = navSaxHandler.getParsedData();
// draw path
drawPath(ds, Color.parseColor("#add331"), mapView);
// find boundary by using itemized overlay
GeoPoint destPoint = new GeoPoint(dest[0],dest[1]);
GeoPoint currentPoint = new GeoPoint(new Double(lastKnownLocation.getLatitude()*1E6).intValue()
,new Double(lastKnownLocation.getLongitude()*1E6).intValue());
Drawable dot = this.getResources().getDrawable(R.drawable.pixel);
MapItemizedOverlay bgItemizedOverlay = new MapItemizedOverlay(dot,this);
OverlayItem currentPixel = new OverlayItem(destPoint, null, null);
OverlayItem destPixel = new OverlayItem(currentPoint, null, null);
bgItemizedOverlay.addOverlay(currentPixel);
bgItemizedOverlay.addOverlay(destPixel);
// center and zoom in the map
MapController mc = mapView.getController();
mc.zoomToSpan(bgItemizedOverlay.getLatSpanE6()*2,bgItemizedOverlay.getLonSpanE6()*2);
mc.animateTo(new GeoPoint(
(currentPoint.getLatitudeE6() + destPoint.getLatitudeE6())/2
, (currentPoint.getLongitudeE6() + destPoint.getLongitudeE6())/2));
} catch(Exception e) {
Log.d("DirectionMap","Exception parsing kml.");
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
// and the rest of the methods in activity, e.g. drawPath() etc...
在你的問題,你說「1的R.layout.main >>存在的主要錯誤。 「什麼是錯誤?至於從StackOverflow問題/答案使用您的參考代碼,該問題的答案說**「請參閱下面的代碼示例,但它並不完整 - 只是爲您作爲參考,並得到一些想法。 「**如果沒有額外的工作來自己填寫缺失的部分,你將無法使用該代碼。 –