当我从当前位置移动时,我正在尝试绘制路线。我在动态绘制路线时遇到了一个大问题,请帮我解决它。我在 map 中的当前位置有标记。一旦我开始移动,我希望 map 开始在我移动的路径上画线。我没有两个固定点。任何人都可以为我提供一个解决方案来解决这个问题。我在 SO 中看到了很多答案,它们在两个固定点之间绘制了路径。但这里只有我的初始点是固定的。我目前能够在我的应用程序中获取我的当前位置。我尝试使用以下代码,但 getLocationManager() 导致错误。我正在使用 Android Studio。
更新代码:
我的 Activity :
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
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.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.maps.android.ui.IconGenerator;
import org.xmlpull.v1.XmlSerializer;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends FragmentActivity implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "MainActivity";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
private String mLastUpdateTime;
private String city = "";
private String country = "";
private String area = "";
private String title;
private String requiredArea = "";
private GoogleMap googleMap;
private List<Address> addresses;
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate ...............................");
//show error dialog if GoolglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
Toast.makeText(this, "Google Play Services is not available", Toast.LENGTH_LONG).show();
finish();
}
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setContentView(R.layout.activity_main);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(getApplicationContext(), "Location button has been clicked", Toast.LENGTH_LONG).show();
return true;
}
});
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart fired ..............");
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop fired ..............");
mGoogleApiClient.disconnect();
Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
Toast.makeText(getApplicationContext(), "Google Play Services is not Available", Toast.LENGTH_LONG).show();
return false;
}
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.d(TAG, "Location update started ..............: ");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Connection failed: " + connectionResult.toString());
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
addMarker();
float accuracy = location.getAccuracy();
Log.d("iFocus", "The amount of accuracy is " + accuracy);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Bundle extras = location.getExtras();
Boolean has = location.hasAccuracy();
String provider = location.getProvider();
Long time = location.getTime();
// Location locationB = new Location("Begur");
// double lati = 12.8723;
// double longi = 77.6329;
// locationB.setLatitude(lati);
// locationB.setLongitude(longi);
// Float distance = location.distanceTo(locationB);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH) + 1;
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
String formattedTime = mDay + ":" + mMonth + ":" + mYear;
Log.d("iFocus", "The name of provider is " + provider);
Log.d("iFocus", "The value of has is " + has);
Log.d("iFocus", "The value of extras is " + extras);
Log.d("iFocus", "The value of Month is " + mMonth);
Log.d("iFocus", "The value of Day is " + mDay);
Log.d("iFocus", "The value of Year is " + mYear);
Log.d("iFocus", "The value of Time is " + formattedTime);
//Log.d("iFocus", "The value of distance is "+distance);
LatLng latLng = new LatLng(latitude, longitude);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
String[] splittedStateName = stateName.split(",");
requiredArea = splittedStateName[2];
Log.d("iFocus", "The value of required area is " + requiredArea);
city = addresses.get(0).getLocality();
area = addresses.get(0).getSubLocality();
String adminArea = addresses.get(0).getAdminArea();
String premises = addresses.get(0).getPremises();
String subAdminArea = addresses.get(0).getSubAdminArea();
String featureName = addresses.get(0).getFeatureName();
String phone = addresses.get(0).getPhone();
country = addresses.get(0).getCountryName();
Log.d("iFocus", "The name of city is " + city);
Log.d("iFocus", "The name of area is " + area);
Log.d("iFocus", "The name of country is " + country);
Log.d("iFocus", "The value of cityName is " + cityName);
Log.d("iFocus", "The value of StateName is " + stateName);
Log.d("iFocus", "The value of CountryName is " + countryName);
Toast.makeText(this, cityName + " " + stateName + " " + countryName, Toast.LENGTH_LONG).show();
SharedPreferences sharedPreferences = getSharedPreferences("MyValues", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("CITY", cityName);
editor.putString("STATE", stateName);
editor.putString("COUNTRY", countryName);
editor.commit();
TextView mapTitle = (TextView) findViewById(R.id.textViewTitle);
if (requiredArea != "" && city != "" && country != "") {
title = mLastUpdateTime.concat(", " + requiredArea).concat(", " + city).concat(", " + country);
}
else {
title = mLastUpdateTime.concat(", " + area).concat(", " + city).concat(", " + country);
}
mapTitle.setText(title);
addMarker();// newly added
final String xmlFile = "userData.xml";
try {
// FileOutputStream fos = new FileOutputStream("userData.xml");
FileOutputStream fos = openFileOutput(xmlFile, Context.MODE_PRIVATE);
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null, "userData");
xmlSerializer.startTag(null, "Time");
xmlSerializer.text(mLastUpdateTime);
xmlSerializer.endTag(null, "Time");
xmlSerializer.startTag(null, "Area");
if (requiredArea != "") {
xmlSerializer.text(requiredArea);
}
else {
xmlSerializer.text(area);
}
xmlSerializer.endTag(null, "Area");
xmlSerializer.startTag(null, "City");
xmlSerializer.text(city);
xmlSerializer.endTag(null, "City");
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite = writer.toString();
fos.write(dataWrite.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dir = getFilesDir().getAbsolutePath();
Log.d("Pana", "The value of Dir is "+dir);
}
private void addMarker() {
MarkerOptions options = new MarkerOptions();
// following four lines requires 'Google Maps Android API Utility Library'
// https://developers.google.com/maps/documentation/android/utility/
// I have used this to display the time as title for location markers
// you can safely comment the following four lines but for this info
IconGenerator iconFactory = new IconGenerator(this);
iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
// options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime + requiredArea + city)));
options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(requiredArea + ", " + city)));
options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
options.position(currentLatLng);
Marker mapMarker = googleMap.addMarker(options);
long atTime = mCurrentLocation.getTime();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
String title = mLastUpdateTime.concat(", " + requiredArea).concat(", " + city).concat(", " + country);
mapMarker.setTitle(title);
TextView mapTitle = (TextView) findViewById(R.id.textViewTitle);
mapTitle.setText(title);
Log.d(TAG, "Marker added.............................");
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
13));
Log.d(TAG, "Zoom done.............................");
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
Log.d(TAG, "Location update stopped .......................");
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
Log.d(TAG, "Location update resumed .....................");
}
}
}
我正在尝试在我的代码中添加此方法来绘制线条,但它在 getLocationManager() 中给出错误;
private void addLocationListener(LocationListener locationListener) {
LocationProvider locationProvider = getLocationManager().getProvider(LocationManager.GPS_PROVIDER);
getLocationManager().requestLocationUpdates(locationProvider.getName(), LOCATION_UPDATE_INTERVAL,
LOCATION_UPDATE_MIN_DISTANCE, locationListener);
}
private LocationManager getLocationManager() {
return (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
}
private void startGpsListening(Location start) {
this.startLocation = start;
addLocationListener(new MyLocationListener());
}
private Location startLocation = new Location("");
private class MyLocationListener extends LocationListener {
public void onLocationChanged(Location location) {
}
...
}
最佳答案
似乎最好的实现是只使用 ArrayList<LatLng>存储 onLocationChanged() 中给出的每个点.然后,每次得到一个新点时,重新画线。
首先,导入绘制线条所需的内容:
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
为 ArrayList 和折线创建成员变量:
private ArrayList<LatLng> points; //added
Polyline line; //added
初始化 points在 onCreate() :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
points = new ArrayList<LatLng>(); //added
//...............
然后,在 onLocationChanged() ,将得到的每个点添加到 ArrayList:
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude); //you already have this
points.add(latLng); //added
redrawLine(); //added
}
取自 this answer ,定义你的redrawLine()方法。
删除对 addMarker() 的所有其他调用, 因为您将调用 clear()在您的 map 上,这会删除所有标记和折线。
private void redrawLine(){
googleMap.clear(); //clears all Markers and Polylines
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
addMarker(); //add Marker in current position
line = googleMap.addPolyline(options); //add Polyline
}
编辑:您可能还希望在位置更改回调之间调用以米为单位的最小距离。
private static final String TAG = "MainActivity";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter
调用 setSmallestDisplacement() :
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
这应该足以让您入门。您可能需要微调位置更改回调的频率以获得所需的结果。可能不止这些,但您可以找到边缘情况并在测试后修复它们。
关于android - 如何在我使用 Google map 从当前位置开始移动时绘制路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249920/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t