【发布时间】:2013-11-28 15:13:32
【问题描述】:
我有一个 onclick_Park() 方法,即使我移动到另一个地方并单击 Park 按钮,它只会在特定位置(即我家的位置)放置一个标记,它会在我家的位置放置一个标记,而不是我现在的位置。
任何帮助都将不胜感激。
这里是 myMainActivity.java 类
package com.example.carfinder;
import java.util.ArrayList;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
public class MainActivity extends android.support.v4.app.FragmentActivity implements LocationListener {
GoogleMap googleMap;
LatLng myPosition ;
LatLng parkingPosition;
GMapV2Direction md;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// if Google Play Services are available then
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
}
}
public void onClick_Clear(View v) {
// Removes all markers, overlays, and polylines from the map.
googleMap.clear();
}
public void onClick_Park(View v){
googleMap.addMarker(new MarkerOptions().position(myPosition).title("Parking Position"));
parkingPosition = myPosition;
}
public void onClick_getDirections(View v){
md = new GMapV2Direction();
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
//org.w3c.dom.Document doc = md.getDocument(myPosition,parkingPosition,
// GMapV2Direction.MODE_DRIVING);
ArrayList<LatLng> directionPoint = md.getDirection(md.getDocument(myPosition,parkingPosition,
GMapV2Direction.MODE_DRIVING));
PolylineOptions rectLine = new PolylineOptions().width(3).color(
Color.RED);
for (int i = 0; i < directionPoint.size(); i++) {
rectLine.add(directionPoint.get(i));
}
Polyline polylin = googleMap.addPolyline(rectLine);
}
public void onClick_Traffic(View v){
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
}
【问题讨论】:
标签: android google-maps google-maps-markers fixed directions