我想在地图中制作多个折线。我在地图中有一条折线,然后我想再次制作新的折线。但该线始终与之前的折线连接。我该怎么办,这是我的代码:
package com.evy;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class Tambah extends FragmentActivity implements LocationListener{
GoogleMap map;
LocationManager lm;
boolean isNewPoly=false;
PolylineOptions polyline;
ArrayList<LatLng> poly = new ArrayList<LatLng>();
ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions polylineOptions;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tambah);
//points=new ArrayList<LatLng>();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.maptambah);
map=mapFragment.getMap();
final LocationListener ll=new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())).title("posisi anda").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), 15.0f));
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
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==true){
MarkerOptions markerOptions = new MarkerOptions().position(point).title("Position").snippet("Latitude: "+point.latitude+" , "+"Longitude: "+point.longitude).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(markerOptions);
polylineOptions = new PolylineOptions().color(Color.BLUE).width(3);
points.add(point);
for(int i=0;i<points.size();i++){
polylineOptions.add(points.get(i));
}
map.addPolyline(polylineOptions);
}
for(int i=0;i<points.size();i++){
poly.add(points.get(i));
}
}
});
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isNewPoly=true;
}
});
stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isNewPoly=false;
}
});
}
public static String encode(final List<LatLng> path){
long lastLat = 0;
long lastLng = 0;
final StringBuffer result = new StringBuffer();
for(final LatLng point:path){
long lat = Math.round(point.latitude * 1e5);
long lng = Math.round(point.longitude * 1e5);
long dlat = lat - lastLat;
long dlng = lng - lastLng;
encoded(dlat, result);
encoded(dlng, result);
lastLat = lat;
lastLng = lng;
}
return result.toString();
}
private static void encoded(long v, StringBuffer result){
v = v < 0 ? ~(v<<1) : v<<1;
while(v >= 0x20){
result.append(Character.toChars((int) ((0x20 | (v & 0x1f))+63)));
v >>= 5;
}
result.append(Character.toChars((int) (v + 63)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tambah, 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.
switch(item.getItemId()){
case R.id.home:
this.finish();
startActivity(new Intent(Tambah.this, Peta.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
如果我按下按钮开始,它会生成新的折线,然后按下按钮停止它,但它不起作用。按钮停止不动摇。
答案 0 :(得分:0)
当前,您一次要附加所有点,而应该使用PolyLine数组。假设坐标1和坐标2是LatLng []类型的数组,这些数组先前是从KML文件或某些其他来源附加的。您应该像下面这样写;
lgn: boolean;
ngOnInit(): void {
this.auth.onAuthStateChanged((user) => {
if (user) {
this.lgn = true;
} else {
this.lgn = false;
}
});
}
答案 1 :(得分:0)
List<LatLng> latLngList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
latLngList.add(i, new LatLng(arrayList.get(i).getLocation_lat(), arrayList.get(i).getLocation_lang()));
}
Polyline polyline = googleMaps.addPolyline(new PolylineOptions()
.clickable(true)
.addAll(latLngList)
.width(2).color(Color.BLUE).geodesic(true));
答案 2 :(得分:-1)
List<LatLng> latLngList = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
latLngList.add(i, new LatLng(arrayList.get(i).getLocation_lat(), arrayList.get(i).getLocation_lang()));
}
Polyline polyline = mMap.addPolyline(new PolylineOptions()
.clickable(true)
.addAll(latLngList)
.width(2).color(Color.BLUE).geodesic(true));