gpt4 book ai didi

java - 像优步一样实现谷歌地图

转载 作者:行者123 更新时间:2023-11-29 20:11:58 24 4
gpt4 key购买 nike

我是 android 的新手。我正在尝试实现像 uber 应用程序这样的可拖动谷歌地图。我从某个地方找到了代码,但是当我尝试运行时,出现了一些问题,比如

Error:(24, 37) error: cannot find symbol class GooglePlayServicesClient Error:(26, 39) error: cannot find symbol class LocationClient

在我的依赖中添加了

compile 'com.google.android.gms:play-services:8.4.0'

以下是我用过的代码。

 package com.rakyow.taxifinder.taxifinder;


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements
ConnectionCallbacks, OnConnectionFailedListener {
// A request to connect to Location Services
private LocationRequest mLocationRequest;
GoogleMap mGoogleMap;


public static String ShopLat;
public static String ShopPlaceId;
public static String ShopLong;
// Stores the current instantiation of the location client in this object
private GoogleApiClient mLocationClient;
boolean mUpdatesRequested = false;
private TextView markerText;
private LatLng center;
private LinearLayout markerLayout;
private Geocoder geocoder;
private List<Address> addresses;
private TextView Address;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
markerText = (TextView) findViewById(R.id.locationMarkertext);
Address = (TextView) findViewById(R.id.adressText);
markerLayout = (LinearLayout) findViewById(R.id.locationMarker);
// Getting Google Play availability status
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());

if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available

int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();

} else { // Google Play Services are available

// Getting reference to the SupportMapFragment
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();

/*
* Set the update interval
*/
mLocationRequest.setInterval(GData.UPDATE_INTERVAL_IN_MILLISECONDS);

// Use high accuracy
mLocationRequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

// Set the interval ceiling to one minute
mLocationRequest
.setFastestInterval(GData.FAST_INTERVAL_CEILING_IN_MILLISECONDS);

// Note that location updates are off until the user turns them on
mUpdatesRequested = false;

/*
* Create a new location client, using the enclosing class to handle
* callbacks.
*/
mLocationClient = new GoogleApiClient(this, this, this);
mLocationClient.connect();
}
}

private void stupMap() {
try {
LatLng latLong;
// TODO Auto-generated method stub
mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();

// Enabling MyLocation in Google Map
mGoogleMap.setMyLocationEnabled(true);
if (mLocationClient.getLastLocation() != null) {
latLong = new LatLng(mLocationClient.getLastLocation()
.getLatitude(), mLocationClient.getLastLocation()
.getLongitude());
ShopLat = mLocationClient.getLastLocation().getLatitude() + "";
ShopLong = mLocationClient.getLastLocation().getLongitude()
+ "";


} else {
latLong = new LatLng(12.9667, 77.5667);
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLong).zoom(19f).tilt(70).build();

mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Clears all the existing markers
mGoogleMap.clear();

mGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

@Override
public void onCameraChange(CameraPosition arg0) {
// TODO Auto-generated method stub
center = mGoogleMap.getCameraPosition().target;

markerText.setText(" Set your Location ");
mGoogleMap.clear();
markerLayout.setVisibility(View.VISIBLE);

try {
new GetLocationAsync(center.latitude, center.longitude)
.execute();

} catch (Exception e) {
}
}
});

markerLayout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {

LatLng latLng1 = new LatLng(center.latitude,
center.longitude);

Marker m = mGoogleMap.addMarker(new MarkerOptions()
.position(latLng1)
.title(" Set your Location ")
.snippet("")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.add_marker)));
m.setDraggable(true);

markerLayout.setVisibility(View.GONE);
} catch (Exception e) {
}

}
});

} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
stupMap();

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onDisconnected() {
// TODO Auto-generated method stub

}

private class GetLocationAsync extends AsyncTask<String, Void, String> {

// boolean duplicateResponse;
double x, y;
StringBuilder str;

public GetLocationAsync(double latitude, double longitude) {
// TODO Auto-generated constructor stub

x = latitude;
y = longitude;
}

@Override
protected void onPreExecute() {
Address.setText(" Getting location ");
}

@Override
protected String doInBackground(String... params) {

try {
geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
addresses = geocoder.getFromLocation(x, y, 1);
str = new StringBuilder();
if (geocoder.isPresent()) {
Address returnAddress = addresses.get(0);

String localityString = returnAddress.getLocality();
String city = returnAddress.getCountryName();
String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();

str.append(localityString + "");
str.append(city + "" + region_code + "");
str.append(zipcode + "");

} else {
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return null;

}

@Override
protected void onPostExecute(String result) {
try {
Address.setText(addresses.get(0).getAddressLine(0)
+ addresses.get(0).getAddressLine(1) + " ");
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected void onProgressUpdate(Void... values) {

}
}

}

我的要求与此类似 Google Maps Uber .请帮我解决这个问题。任何帮助将不胜感激。提前致谢

最佳答案

我建议您使用 GoogleApiClient,因为如果我没记错的话,较新版本的播放服务不再支持 LocationClient,他们从 Google Play 服务 6.5.87 开始将其删除。这是 example implementation 的链接.如果您真的想使用我不推荐的 LocationClient,您可以使用旧版本的 play-services

关于java - 像优步一样实现谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34629012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com