我的问题不断迷失,我真的需要帮助,现在已经卡住了2个星期没有任何运气:( 我的应用程序基本上是一个地图,其中包含一个XML文件放在其上的标记,该文件存储在res \ raw \ markers.xml中。地图加载正常并显示我当前的位置,Logcat正在记录我的xml数据,因此我只能假设它正在解析正常,但地图上没有显示任何标记。我不得不使用SAX Parser,因为当我使用xmlPullParser时,它在启动时非常缓慢且无响应。我只是不知道为什么SAX Parser没有显示任何标记但我的xmlPullParser没有。我的XML文件中有大约7000多行。
任何人都可以帮助我吗?提前致谢
MainActivity:
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import com.androidmapsextensions.ClusteringSettings;
import com.androidmapsextensions.GoogleMap.OnMarkerClickListener;
import com.androidmapsextensions.MapView;
import com.androidmapsextensions.Marker;
import com.androidmapsextensions.MarkerOptions;
import com.androidmapsextensions.SupportMapFragment;
import com.androidmapsextensions.GoogleMap;
import android.support.v4.app.FragmentActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class MainActivity extends FragmentActivity implements OnMarkerClickListener, LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationSource {
private OnLocationChangedListener mListener;
private LocationManager locationmanager;
GoogleMap mMap;
LatLng myposition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(locationmanager != null){
boolean gpsIsEnabled = locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationmanager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsIsEnabled){
locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);
}
else if(networkIsEnabled)
{
locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this);
}
else
{
Toast.makeText(this, "GPS is not enabled", Toast.LENGTH_SHORT).show();
}
setUpMapIfNeeded();
}
}
@Override
public void onPause()
{
if(locationmanager != null){
locationmanager.removeUpdates(this);
}
super.onPause();
}
@Override
public void onResume(){
super.onResume();
setUpMapIfNeeded();
if(locationmanager != null){
mMap.setMyLocationEnabled(true);
}
}
private void setUpMapIfNeeded() {
if (mMap == null){
mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.the_map)).getExtendedMap();
if (mMap != null){
setUpMap();
}
mMap.setLocationSource(this);
mMap.setMyLocationEnabled(true);
showmypoi();
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMarkerClickListener(this);
}
private void showmypoi() {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
InputStream localfile;
localfile = getResources().openRawResource(R.raw.markers);
XMLHandlers myXMLHandler = new XMLHandlers();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(localfile));
}
catch (Exception e){
System.out.println("XML Parsing Exception = " + e);
e.printStackTrace();
}
for(int i = 0; i < Locations.size(); i++){
Double latitude = Double.valueOf(Locations.size());
Double longitude = Double.valueOf(Locations.size());
LatLng Lng = new LatLng(latitude,longitude);
mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.the_map)).getExtendedMap();
String icon = null;
mMap.getMarkers();
mMap.addMarker(new MarkerOptions().position(Lng).icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier(icon, "drawable", getPackageName()))));
}
}
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
}
@Override
public void deactivate() {
mListener = null;
}
@Override
public void onConnectionFailed(ConnectionResult result) {}
@Override
public void onConnected(Bundle connectionHint) {}
@Override
public void onDisconnected() {}
public void onCameraChange(CameraPosition cameraPosition){
}
@Override
public void onLocationChanged(Location location) {
if( mListener != null){
mListener.onLocationChanged( location );
LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
if(!bounds.contains(new LatLng(location.getAltitude(), location.getLongitude())));{
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "No Network Data Available", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Using Network Data", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(this, "GPS Data Used", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onMarkerClick(Marker marker) {
double latitude = marker.getPosition().latitude;
double longitude = marker.getPosition().longitude;
double latitudeCurr = mMap.getMyLocation().getLatitude();
double longitudeCurr = mMap.getMyLocation().getLongitude();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://maps.google.co.uk/maps?" + "saddr="
+ latitudeCurr + "," + longitudeCurr + "&daddr="
+ latitude + "," + longitude + "&mode=driving"));
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException ex)
{
try
{
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://maps.google.co.uk/maps?" + "saddr="
+ latitudeCurr + "," + longitudeCurr + "&daddr="
+ latitude + "," + longitude + "&mode=driving"));
startActivity(unrestrictedIntent);
}
catch(ActivityNotFoundException innerEx)
{
Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
}
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId() == R.id.action_legalnotices){
startActivity(new Intent(this, LegalNoticesActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
XMLHandler:
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLHandlers extends DefaultHandler {
ArrayList<Locations> locations = null;
private Locations loc = null;
public ArrayList<Locations> getLocations(){
return locations;
}
boolean bLat = false;
boolean bLng = false;
boolean bIcon = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("marker")){
loc = new Locations();
if (locations == null)
locations = new ArrayList<Locations>();
} else if (qName.equalsIgnoreCase("lat")){
bLat = true;
} else if (qName.equalsIgnoreCase("lng")){
bLng = true;
} else if (qName.equalsIgnoreCase("icon")){
bIcon = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("marker")) {
locations.add(loc);
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (bIcon){
loc.setIcon(new String(ch, start, length));
bIcon = false;
}
}
}
XMLArray类:
import android.util.Log;
public class Locations {
private double lat;
private double lng;
private String icon;
public double getLat(){
return lat;
}
public void setLat(double lat){
this.lat = lat;
}
public double getLng(){
return lng;
}
public void setLng(double lng){
this.lng = lng;
}
public String getIcon(){
return icon;
}
public void setIcon(String icon){
this.icon = icon;
Log.i("....", icon);
}
public static int size() {
return 0;
}
}
XML布局:
<lat>51.60002</lat>
<lng>0.518862</lng>
<icon>motel</icon>
</marker>
<marker>
<lat>51.61325</lat>
<lng>0.524572</lng>
<icon>hotel</icon>
</marker>
<marker>
<lat>51.591483</lat>
<lng>0.502149</lng>
<icon>hotel</icon>
</marker>
<marker>
<lat>51.590816</lat>
<lng>0.473675</lng>
<icon>bnb</icon>
</marker>
</markers>
答案 0 :(得分:0)
哇...我希望Stackoverflow的格式化而不是你的格式化。我可以在你的代码中看到很多潜在的错误,但我现在没有时间指出它们,相关的相关内容是:
for(int i = 0; i < Locations.size(); i++){
// Add marker here...
}
Locations
是一个类,而不是一个位置列表。使用名为size()
的静态方法,该方法始终返回0
public static int size() {
return 0;
}
我认为您真正想要的是位置列表,即List<Location> locations
,然后您可以致电locations.size()
如有疑问 - 打印出所有内容!如果您在for循环中添加了Log.d('Tag', 'index: ' + i);
,您会发现它永远不会被执行,并且您知道我从未&gt; 0