迫切需要为android项目添加方向
我已经思考和上网了几天了,但是在大约一周的工作效率之后,我决定问这个模糊的问题。我的问题-有人能告诉我如何在我的android项目中实现方向吗 为了详细说明,我在这里要求的确切功能将在下一段中强调 我的项目的目标是:每当用户在某个位置键入内容时(单击“转到”后它将隐藏键盘),它将转到该位置并在那里放置一个标记然后它将显示当前位置和标记之间行程时间最短的路线。我不知道如何限制用户可以搜索位置所在的国家,但我确信这很容易。我已经有了我的最终目标,但不是我真正需要的 最后,这是我的主要活动。我在这里引用了一些我没有添加的类,因此,只需询问您是否需要它们在自己的编译器中运行此代码:迫切需要为android项目添加方向,android,google-maps,google-maps-markers,google-maps-api-2,google-directions-api,Android,Google Maps,Google Maps Markers,Google Maps Api 2,Google Directions Api,我已经思考和上网了几天了,但是在大约一周的工作效率之后,我决定问这个模糊的问题。我的问题-有人能告诉我如何在我的android项目中实现方向吗 为了详细说明,我在这里要求的确切功能将在下一段中强调 我的项目的目标是:每当用户在某个位置键入内容时(单击“转到”后它将隐藏键盘),它将转到该位置并在那里放置一个标记然后它将显示当前位置和标记之间行程时间最短的路线。我不知道如何限制用户可以搜索位置所在的国家,但我确信这很容易。我已经有了我的最终目标,但不是我真正需要的 最后,这是我的主要活动。我在这里引
public class MainActivity extends FragmentActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
@SuppressWarnings("unused")
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9002;
@SuppressWarnings("unused")
private static final String LOGTAG = "Maps";
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
private static final float DEFAULTZOOM = 15;
private GoogleApiClient mGoogleApiClient;
private LocationListener mListener;
private Marker marker;
ArrayList<LatLng> mMarkerPoints;
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_main);
if (initMap()) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
} else {
Toast.makeText(this, "Hmmm. Maps didn't load.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "There's something wrong with your play services", Toast.LENGTH_SHORT).show();
}
UiSettings config = mMap.getUiSettings();
config.setMapToolbarEnabled(false);
config.setZoomControlsEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.legal:
Intent intent = new Intent(this, LicenseActivity.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mListener);
}
@Override
protected void onResume() {
super.onResume();
MapStateManager mgr = new MapStateManager(this);
CameraPosition position = mgr.getSavedCameraPosition();
if (position != null) {
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
mMap.moveCamera(update);
mMap.setMapType(mgr.getSavedMapType());
}
}
@Override
protected void onStop() {
super.onStop();
MapStateManager mgr = new MapStateManager(this);
mgr.saveMapState(mMap);
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
MapFragment mapFrag =
(MapFragment) getFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
return (mMap != null);
}
@SuppressWarnings("unused")
private void gotoLocation(double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mMap.moveCamera(update);
}
private void gotoLocation(double lat, double lng,
float zoom) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
public void geoLocate(View v) throws IOException {
EditText et = (EditText) findViewById(R.id.editText1);
String location = et.getText().toString();
if (location.length() == 0) {
Toast.makeText(this, "Please enter a location", Toast.LENGTH_SHORT).show();
return;
}
hideSoftKeyboard(v);
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, DEFAULTZOOM);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions()
.position(new LatLng(lat, lng));
marker = mMap.addMarker(options);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
public void showCurrentLocation(MenuItem item) {
int permCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
if (permCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
Location currentlocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (currentlocation == null) {
Toast.makeText(this, "Couldn't find you!", Toast.LENGTH_SHORT).show();
} else {
LatLng latlng = new LatLng(
currentlocation.getLatitude(),
currentlocation.getLongitude()
);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
latlng, 15
);
mMap.animateCamera(update);
}
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
int permCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
if (permCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
Toast.makeText(this, "Go go go!", Toast.LENGTH_SHORT).show();
mListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
gotoLocation(location.getLatitude(), location.getLongitude(), 15);
}
};
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(20000);
request.setFastestInterval(0);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, mListener
);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
public类MainActivity扩展了FragmentActivity
实现GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener{
@抑制警告(“未使用”)
专用静态最终int连接故障解决请求=9002;
@抑制警告(“未使用”)
私有静态最终字符串LOGTAG=“Maps”;
私人静态最终内部GPS错误对话框请求=9001;
私有静态最终浮点默认缩放=15;
私人GoogleapClient MGoogleapClient;
私人场所监听器;
专用标记;
ArrayList mMarkerPoints;
谷歌地图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(servicesOK()){
setContentView(R.layout.activity_main);
if(initMap()){
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
}否则{
Toast.makeText(此“Hmmm.Maps未加载”)、Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(这是“您的播放服务有问题”,Toast.LENGTH_SHORT.show();
}
UiSettings config=mMap.getUiSettings();
config.setMapToolbarEnabled(false);
config.setZoomControlsEnabled(false);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.legal:
意向意向=新意向(此为LicenseActivity.class);
星触觉(意向);
打破
违约:
打破
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
@凌驾
受保护的void onPause(){
super.onPause();
LocationServices.FusedLocationApi.RemovelocationUpdate(MGoogleapClient,mListener);
}
@凌驾
受保护的void onResume(){
super.onResume();
MapStateManager mgr=新的MapStateManager(此);
CameraPosition position=mgr.getSavedCameraPosition();
如果(位置!=null){
CameraUpdate update=CameraUpdateFactory.newCameraPosition(位置);
移动摄像头(更新);
mMap.setMapType(mgr.getSavedMapType());
}
}
@凌驾
受保护的void onStop(){
super.onStop();
MapStateManager mgr=新的MapStateManager(此);
saveMapState(mMap)经理;
if(mgoogleapClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
公共布尔服务sok(){
int isAvailable=GooglePlayServicesUtil.isGooglePlayServicesAvailable(此);
if(isAvailable==ConnectionResult.SUCCESS){
返回true;
}else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog Dialog=GooglePlayServicesUtil.getErrorDialog(此为GPS\u ERRORDIALOG\u请求可用);
dialog.show();
}否则{
Toast.makeText(这是“无法连接到Google Play服务”,Toast.LENGTH_SHORT.show();
}
返回false;
}
私有布尔initMap(){
如果(mMap==null){
地图碎片=
(MapFragment)getFragmentManager().findFragmentById(R.id.map);
mMap=mapFrag.getMap();
}
返回值(mMap!=null);
}
@抑制警告(“未使用”)
私人空位(双lat、双lng){
LatLng ll=新LatLng(lat,lng);
CameraUpdate update=CameraUpdateFactory.newLatLng(ll);
移动摄像头(更新);
}
私人空位(双lat、双lng、,
浮动(缩放){
LatLng ll=新LatLng(lat,lng);
CameraUpdate update=CameraUpdateFactory.newLatLngZoom(ll,缩放);
移动摄像头(更新);
}
公共void地理定位(视图v)引发IOException{
EditText et=(EditText)findViewById(R.id.editText1);
字符串位置=et.getText().toString();
if(location.length()==0){
Toast.makeText(这是“请输入位置”,Toast.LENGTH_SHORT.show();
返回;
}
隐藏键盘(v);
地理编码器gc=新地理编码器(本);
List List=gc.getFromLocationName(位置,1);
地址添加