在我的应用程序中,我在地图上显示了2个位置,并用标记对其进行了标记。现在,我想绘制它们之间的路线,我不知道我该怎么做。我的功能画得怎么样?
这是我的代码:
package com.ShoppingList.Maps;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.ShoppingList.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import java.util.ArrayList;
import java.util.List;
public class OnMap extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;
//private myOverlay m = null;
double latitudine;
double longitudine;
double latshop;
double longshop;
String nameshop;
Canvas canvas = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shopsonmap);
map = (MapView) findViewById(R.id.shopsonmap);
latitudine = getIntent().getDoubleExtra("latcurent", 0);
longitudine = getIntent().getDoubleExtra("longcurent", 0);
latshop = getIntent().getDoubleExtra("latshop", 0);
longshop = getIntent().getDoubleExtra("longshop", 0);
nameshop = getIntent().getStringExtra("nameshop");
GeoPoint p1 = new GeoPoint((int) latitudine, (int) longitudine);
GeoPoint p2 = new GeoPoint((int) latshop, (int) longshop);
map.getController().setCenter(getPoint(latitudine, longitudine));
map.getController().setZoom(15);
map.setBuiltInZoomControls(true);
map.setSatellite(false);
map.setStreetView(true);
map.invalidate();
Drawable marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me = new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
/*class myOverlay extends Overlay {
GeoPoint gp1;
GeoPoint gp2;
public myOverlay(GeoPoint gp1, GeoPoint gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint mPaint = new Paint();
Point from = new Point();
projection.toPixels(gp1, from);
mPaint.setColor(Color.BLUE);
Point to = new Point();
projection.toPixels(gp2, to);
mPaint.setStrokeWidth(9);
mPaint.setAlpha(120);
canvas.drawLine(from.x, from.y, to.x, to.y, mPaint);
super.draw(canvas, mapView, shadow);
}
}*/
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
@Override
protected boolean isRouteDisplayed() {
return (false);
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
private Drawable marker = null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker = marker;
items.add(new OverlayItem(getPoint(latitudine, longitudine),
"Your location", "You are here!"));
items.add(new OverlayItem(getPoint(latshop, longshop), "The shop",
"The shop " + nameshop + " is here"));
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(OnMap.this, items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return (true);
}
@Override
public int size() {
return (items.size());
}
}
}
谢谢..
答案 0 :(得分:1)
因此,假设您从REST Web服务获取位置(以JSON格式)。为此,我使用Volley库连接并从服务器获取响应。
JSONArray响应示例:
[{...,&#34; location&#34;:&#34; 44.924654,8.586219&#34;,...}, {...,&#34; location&#34;:&#34; 44.906177,8.157752&#34;,...}, {...,&#34; location&#34;:&#34; 44.906177,8.157752&#34;,...},{...,&#34; location&#34;: &#34; 44.956733,7.876227&#34;,...},{...,&#34; location&#34;:&#34; 45.034424,7.671607&#34;, ......}
步骤是将第一个和最后一个位置设置为标记,中间位置将在它们之间绘制线条。
因为location是以字符串形式获得的,所以我们首先拆分字符串并在&#34;,&#34;之前分配部分。纬度和其余经度。
public void onResponse(JSONArray response) {
if (response.length() > 0) {
try {
//creating the markers: for this I need the first and the last location
JSONObject firstLocationJson = response.getJSONObject(0);
JSONObject lastLocationJson = response.getJSONObject(response.length() - 1);
String[] firstLocationLatLng = firstLocationJson.getString("location").split(",");
Location firstLocation = new Location(LocationManager.GPS_PROVIDER);
firstLocation.setLatitude(Double.parseDouble(firstLocationLatLng[0]));
firstLocation.setLongitude(Double.parseDouble(firstLocationLatLng[1]));
String[] lastLocationLatLng = lastLocationJson.getString("location").split(",");
Location lastLocation = new Location(LocationManager.GPS_PROVIDER);
lastLocation.setLatitude(Double.parseDouble(lastLocationLatLng[0]));
lastLocation.setLongitude(Double.parseDouble(lastLocationLatLng[1]));
final float distance = firstLocation.distanceTo(lastLocation); //distance in meters
if (distance > 50000 && distance < 200000) { //distance bigger than 50 km
showMapView(response, firstLocation, lastLocation, 7);
} else if (distance > 300000) {
showMapView(response, firstLocation, lastLocation, 5);
}
} catch (JSONException e) {
// TODO
}
}
// TODO -
}
现在让我们看一下绘制MapView的方法。请注意,我不在活动中,如果我想强制代码在主线程上运行(用于更新UI),我将使用Handler和Runnable。 方法showMapView()是一个负责绘制标记和中间位置的方法。
private void showMapView(JSONArray response, Location firstLoc, Location lastLoc, final int zoom) {
final LatLng latLng1 = new LatLng(firstLoc.getLatitude(), firstLoc.getLongitude());
final LatLng latLng2 = new LatLng(lastLoc.getLatitude(), lastLoc.getLongitude());
final MarkerOptions marker1 = new MarkerOptions().position(latLng1);
final MarkerOptions marker2 = new MarkerOptions().position(latLng2);
final PolylineOptions polylineOptions = new PolylineOptions();
final ArrayList<LatLng> points = new ArrayList<LatLng>();
//saving all the locations in an ArrayList
if (response.length() > 0) {
for (int i = 0; i < response.length(); i++) {
JSONObject locationsJson = null;
try {
locationsJson = response.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
String locationString = null;
try {
locationString = locationsJson.getString("location");
} catch (JSONException e) {
e.printStackTrace();
}
//here I am splitting the location string in a String array.
String[] locationLatLng = locationString.split(",");
Location loc = new Location(LocationManager.GPS_PROVIDER);
loc.setLatitude(Double.parseDouble(locationLatLng[0]));
loc.setLongitude(Double.parseDouble(locationLatLng[1]));
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
points.add(latLng);
}
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(marker1);
googleMap.addMarker(marker2);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng1, zoom));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2, zoom));
polylineOptions.addAll(points);
polylineOptions.width(10);
polylineOptions.color(Color.BLUE);
googleMap.addPolyline(polylineOptions);
}
});
}
};
mainHandler.post(myRunnable);
}
代码清晰明了,点(中间位置)是使用PolylineOptions类型的对象绘制的,并使用以下行添加到地图中:googleMap.addPolyline(polylineOptions);
所需的缩放级别,范围为2.0到21.0。低于此范围的值设置为2.0,高于此值的值设置为21.0。增加值以放大。并非所有区域都具有最大缩放级别的图块。 read here about zoom
答案 1 :(得分:-1)