如何将地图路径状态传递给另一个活动android谷歌地图

时间:2015-04-11 20:20:26

标签: android google-maps

在我的应用程序用户应该标记起始点然后应绘制路径,然后当用户单击按钮时,此路径应显示在新活动中 如何在另一个活动中显示此路径 这是我的代码 抱歉英语不好

package com.example.project;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;



import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapFragment;
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 java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class  AirportActivity extends Activity implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener{    
    boolean markerClicked;
     PolylineOptions rectOptions;
     Polyline polyline;
      GoogleMap googleMap;
      List<LatLng> points;
     List<Polyline> polylines;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.airportactivity);

    googleMap = ((MapFragment)getFragmentManager().findFragmentById(
            R.id.mapView)).getMap();
    //googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//  googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));
    points = new ArrayList<LatLng>();
    polylines = new ArrayList<Polyline>();
     String[] placeNames = {"Cairo International Airport","Alexandria International Airport","Borg El Arab Airport",
            "Marsa Matrouh Airport","Sharm el-Sheikh International Airport","Taba International Airport","El Kharga Airport",
            "Assiut Airport","Luxor International Airport","Aswan International Airport","El Arish International Airport",
            "St. Catherine International Airport","Sharq Al-Owainat Airport","Abu Simbel Airport","Sohag International Airport",
            "Port Said Airport","El Tor Airport","Dakhla Oasis Airport","Marsa Alam International Airport","Cairo West Air Base","Almaza Air Force Base"};
     String[] placeNamesSnippet = {"Cairo International Airport1","Alexandria International Airport2","Borg El Arab Airport3", "Marsa Matrouh Airport4","Sharm el-Sheikh International Airport5","Taba International Airport6","El Kharga Airport7", "Assiut Airport8","Luxor International Airport9","Aswan International Airport10","El Arish International Airport11", "St. Catherine International Airport12","Sharq Al-Owainat Airport13","Abu Simbel Airport14","Sohag International Airport15", "Port Said Airport16","El Tor Airport17","Dakhla Oasis Airport18","Marsa Alam International Airpor19t","Cairo West Air Bas20e","Almaza Air Force Base21"};
     Double[] placeLatitude =  {30.111370, 31.192553, 30.917138,31.324435,27.978620,29.590988,27.188222,27.047695, 25.670264,
            23.960397,31.076449,28.684537,22.580600,22.375813,26.331926,31.281150,28.208842,25.688581,25.558141,
            30.116704,30.095975};
     Double[] placeLongitude = {31.413910, 29.953141,29.693375, 27.222200,34.393354,34.778946 , 33.800840, 31.013473 , 32.704063,
            32.821163,33.832256,34.062882, 28.720754, 31.611667,31.728437,32.242223,33.645257,28.972356,34.582821,
            30.916667,31.362748};
    for(int i = 0 ; i<21;i++)
      {
          googleMap.addMarker(new MarkerOptions()
                  .snippet(placeNamesSnippet[i])
                  .position(new LatLng(placeLatitude[i], placeLongitude[i]))
                  .title(placeNames[i]));

        points.add(new LatLng(placeLatitude[i], placeLongitude[i]));

 markerClicked = false;
}    
     markerClicked = false; 
      googleMap.setOnMapLongClickListener(this);
      googleMap.setOnMarkerClickListener(this);


}


@Override
public boolean onMarkerClick(Marker marker) {
    if(points.size() > 2)
    {
        FindShortestPath();
    }
    else if(points.size() == 2)
    { if(markerClicked){

  if(polyline != null){
   polyline.remove();
   polyline = null;
  }
  rectOptions.add(marker.getPosition());
  rectOptions.color(Color.RED);
  polyline = googleMap.addPolyline(rectOptions);
 }else{
  if(polyline != null){
   polyline.remove();
   polyline = null;
  }

  rectOptions = new PolylineOptions().add(marker.getPosition());
  markerClicked = true;
 }
}
 return true;
}
public void onClick(View view) {
    startActivity(new Intent("net.learn2develop.ListMapActivity"));
    }    
public static double distance(LatLng StartP, LatLng EndP) {
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2 * Math.asin(Math.sqrt(a));
    return 6366000 * c;
}
// calculate distances matrix then get shortest path then call draw
public void FindShortestPath()
{
TSPNearestNeighbour tsp = new TSPNearestNeighbour();
int adjacencyMatrix[][] = new int[points.size()][];
int counter = 0,innerCounter ;
int[] result = new int[points.size()];

for(LatLng point : points)
{
    innerCounter=0;
    adjacencyMatrix[counter] = new int[points.size()];
    for(LatLng point1 : points)
    {
        if(point.equals(point1))
        {
            adjacencyMatrix[counter][innerCounter] =  0;
        }
        else 
            adjacencyMatrix[counter][innerCounter] =(int) distance(point,point1);
            innerCounter ++;
    }
    counter ++;
}
result = tsp.tsp(adjacencyMatrix);
DrawShortestPath(result);
}
//draws the shortest path with markers 
public void DrawShortestPath(int[] path)
{

    googleMap.clear();

int numOfNodes = points.size();
for(LatLng point : points)
{
    googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
               .position(point));
}
for (int index = 0 ; index < numOfNodes-1 ; index++)
{
     googleMap.addPolyline(new PolylineOptions()
.add(points.get(path[index]), points.get(path[index+1]))
.width(3)
.color(Color.RED));
}
}
class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;

public TSPNearestNeighbour()
{
    stack = new Stack<Integer>();
}

public int[] tsp(int adjacencyMatrix[][])
{
    numberOfNodes = adjacencyMatrix[0].length ;
    int[] result = new int[adjacencyMatrix[0].length];
    int resultCounter = 1;
    int[] visited = new int[numberOfNodes];
    visited[0] = 1;
    stack.push(0);
    int element, dst = 0, i;
    int min = Integer.MAX_VALUE;
    boolean minFlag = false;
    result[0] = 0;

    //System.out.print(1 + "\t");

    while (!stack.isEmpty())
    {
        element = stack.peek();
        i = 0;
        min = Integer.MAX_VALUE;
        while (i < numberOfNodes)
        {
            if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
            {
                if (min > adjacencyMatrix[element][i])
                {
                    min = adjacencyMatrix[element][i];
                    dst = i;
                    minFlag = true;
                }
            }
            i++;
        }
        if (minFlag)
        {
            visited[dst] = 1;
            stack.push(dst);
            result[resultCounter] = dst;
            resultCounter++;
            //System.out.print(dst + "\t");
            minFlag = false;
            continue;
        }
        stack.pop();
    }
    return result;
}

}
@Override
public void onMapClick(LatLng arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onMapLongClick(LatLng arg0) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:0)

如果您已将信息作为数组,则可以将其作为额外内容传递给您用于启动新活动的Intent

像这样修改你的点击:

public void onClick(View view) {
Intent i = new Intent("net.learn2develop.ListMapActivity");
i.putExtra("latitudes", placeLatitude);
i.putExtra("longitudes", placeLongitude);
//call putExtra(key, value) for each array you want to pass to the other activity
startActivity(i);
} 

您可以阅读有关在活动hereofficial documentation之间传递值的详情。