Android:如何知道地图API V2中的活动折线

时间:2014-12-07 23:34:09

标签: android google-maps

我有应用程序在android地图中制作折线。我用onMapLongClickListener检查了aktive折线。我检查了存储折线路径的arraylist。问题是,当我有两条分离的折线并且我想检查最近的路径时,我只能用我制作的最后一条折线检查它,我想检查第一条折线,但它不能正常工作。我的代码有什么问题?



package com.evy;

import java.util.ArrayList;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.support.v4.app.FragmentActivity;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
	GoogleMap map;
	private boolean isNewPoly=false;
	ArrayList<LatLng> points;
	PolylineOptions polylineOptions;
	String list;
	MarkerOptions markerOptions;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		SupportMapFragment mapFragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
		map=mapFragment.getMap();
		
		Button stop = (Button) findViewById(R.id.stop);
		Button start = (Button) findViewById(R.id.start);
		
		map.setOnMapClickListener(new OnMapClickListener(){

			@Override
			public void onMapClick(LatLng point) {
				// TODO Auto-generated method stub
				if(isNewPoly){
					markerOptions = new MarkerOptions().position(point).title("Position").snippet("Latitude: "+point.latitude+" , "+"Longitude: "+point.longitude);
					map.addMarker(markerOptions);
					points.add(point);
					polylineOptions = new PolylineOptions();
					polylineOptions.color(Color.BLUE);
					polylineOptions.width(3);
					polylineOptions.addAll(points);
					map.addPolyline(polylineOptions);
				}
				else {
					
					cek(point);
					
				}
			}
		});
		
		map.setOnMapLongClickListener(new OnMapLongClickListener(){

			@Override
			public void onMapLongClick(LatLng poin) {
				// TODO Auto-generated method stub
				//if(isNewPoly){
					cek(poin);
				//}
			}
			
		});
		
		start.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				points=new ArrayList<LatLng>();
				isNewPoly=true;
			}
		});
		
		stop.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
	            isNewPoly = false;
			}
		});
	}
	
	public void cek(LatLng poin){
		float mindist = 0;
		int pos=0;
		Double lat1=poin.latitude;
		Double lng1=poin.longitude;
		
		for(int i=0; i<points.size();i++){
			Double lat=points.get(i).latitude;
			Double lng=points.get(i).longitude;
			float[] distance = new float [1];
			Location.distanceBetween(lat1, lng1, lat, lng, distance);
			if(i==0){
				mindist=distance[0];
			}
			else if(mindist>distance[0]){
				mindist=distance[0];
				pos=i;
			}
		}
		LatLng ne = points.get(pos);
		Toast.makeText(MainActivity.this, "marker terdekat "+ne+" "+mindist, Toast.LENGTH_LONG).show();
		MarkerOptions markerOptions = new MarkerOptions().position(poin).title("Position").snippet("Latitude: "+lat1+" , "+"Longitude: "+lng1);
		map.addMarker(markerOptions);
		points=new ArrayList<LatLng>();
		points.add(ne);
		
		points.add(poin);
		polylineOptions = new PolylineOptions();
		polylineOptions.color(Color.BLUE);
		polylineOptions.width(3);
		polylineOptions.addAll(points);
		map.addPolyline(polylineOptions);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
&#13;
&#13;
&#13;

这就是我制作的代码。感谢

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要一种方法来跟踪输入到地图中的不同折线。 map.addPolyline(polylineOptions)将返回Polyline对象。您可以将这些折线存储在一个数组中,以便以后进行比较。可以在每个Polyline对象上调用getPoints()以获取LatLng点。