0
我有應用程序在android地圖中製作多段線。我用onMapLongClickListener檢查動態多段線。我檢查存儲折線路徑的ArrayList。問題是,當我有兩條分開的多段線並且我想檢查最近的路徑時,我只能用最後一條多段線檢查它,我想檢查第一條多段線,但它不起作用。我的代碼有什麼問題?Android:如何知道地圖中的活動多段線API V2
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 {
\t GoogleMap map;
\t private boolean isNewPoly=false;
\t ArrayList<LatLng> points;
\t PolylineOptions polylineOptions;
\t String list;
\t MarkerOptions markerOptions;
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.activity_main);
\t \t
\t \t SupportMapFragment mapFragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
\t \t map=mapFragment.getMap();
\t \t
\t \t Button stop = (Button) findViewById(R.id.stop);
\t \t Button start = (Button) findViewById(R.id.start);
\t \t
\t \t map.setOnMapClickListener(new OnMapClickListener(){
\t \t \t @Override
\t \t \t public void onMapClick(LatLng point) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t if(isNewPoly){
\t \t \t \t \t markerOptions = new MarkerOptions().position(point).title("Position").snippet("Latitude: "+point.latitude+" , "+"Longitude: "+point.longitude);
\t \t \t \t \t map.addMarker(markerOptions);
\t \t \t \t \t points.add(point);
\t \t \t \t \t polylineOptions = new PolylineOptions();
\t \t \t \t \t polylineOptions.color(Color.BLUE);
\t \t \t \t \t polylineOptions.width(3);
\t \t \t \t \t polylineOptions.addAll(points);
\t \t \t \t \t map.addPolyline(polylineOptions);
\t \t \t \t }
\t \t \t \t else {
\t \t \t \t \t
\t \t \t \t \t cek(point);
\t \t \t \t \t
\t \t \t \t }
\t \t \t }
\t \t });
\t \t
\t \t map.setOnMapLongClickListener(new OnMapLongClickListener(){
\t \t \t @Override
\t \t \t public void onMapLongClick(LatLng poin) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t //if(isNewPoly){
\t \t \t \t \t cek(poin);
\t \t \t \t //}
\t \t \t }
\t \t \t
\t \t });
\t \t
\t \t start.setOnClickListener(new OnClickListener(){
\t \t \t @Override
\t \t \t public void onClick(View arg0) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t points=new ArrayList<LatLng>();
\t \t \t \t isNewPoly=true;
\t \t \t }
\t \t });
\t \t
\t \t stop.setOnClickListener(new OnClickListener(){
\t \t \t @Override
\t \t \t public void onClick(View arg0) {
\t \t \t \t // TODO Auto-generated method stub
\t isNewPoly = false;
\t \t \t }
\t \t });
\t }
\t
\t public void cek(LatLng poin){
\t \t float mindist = 0;
\t \t int pos=0;
\t \t Double lat1=poin.latitude;
\t \t Double lng1=poin.longitude;
\t \t
\t \t for(int i=0; i<points.size();i++){
\t \t \t Double lat=points.get(i).latitude;
\t \t \t Double lng=points.get(i).longitude;
\t \t \t float[] distance = new float [1];
\t \t \t Location.distanceBetween(lat1, lng1, lat, lng, distance);
\t \t \t if(i==0){
\t \t \t \t mindist=distance[0];
\t \t \t }
\t \t \t else if(mindist>distance[0]){
\t \t \t \t mindist=distance[0];
\t \t \t \t pos=i;
\t \t \t }
\t \t }
\t \t LatLng ne = points.get(pos);
\t \t Toast.makeText(MainActivity.this, "marker terdekat "+ne+" "+mindist, Toast.LENGTH_LONG).show();
\t \t MarkerOptions markerOptions = new MarkerOptions().position(poin).title("Position").snippet("Latitude: "+lat1+" , "+"Longitude: "+lng1);
\t \t map.addMarker(markerOptions);
\t \t points=new ArrayList<LatLng>();
\t \t points.add(ne);
\t \t
\t \t points.add(poin);
\t \t polylineOptions = new PolylineOptions();
\t \t polylineOptions.color(Color.BLUE);
\t \t polylineOptions.width(3);
\t \t polylineOptions.addAll(points);
\t \t map.addPolyline(polylineOptions);
\t \t
\t }
\t
\t @Override
\t public boolean onCreateOptionsMenu(Menu menu) {
\t \t // Inflate the menu; this adds items to the action bar if it is present.
\t \t getMenuInflater().inflate(R.menu.main, menu);
\t \t return true;
\t }
\t @Override
\t public boolean onOptionsItemSelected(MenuItem item) {
\t \t // Handle action bar item clicks here. The action bar will
\t \t // automatically handle clicks on the Home/Up button, so long
\t \t // as you specify a parent activity in AndroidManifest.xml.
\t \t int id = item.getItemId();
\t \t if (id == R.id.action_settings) {
\t \t \t return true;
\t \t }
\t \t return super.onOptionsItemSelected(item);
\t }
}
,這是我做的代碼。謝謝
你能用代碼解釋嗎?我想通過點擊地圖來檢查折線的近點。如果在當前位置附近有一個點,我希望它創建新的折線。你明白我的意思嗎?對不起,我的英語不好 – 2014-12-19 04:25:45