緯度と経度の2つの間にパスを描画しようとしています。これが私のMapsActivity.Javaです。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
find = (Button) findViewById(R.id.btnFindPath);
or = (EditText) findViewById(R.id.etOrigin);
dest = (EditText) findViewById(R.id.etDestination);
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendRequest();
}
});
}
public void sendRequest(){
String Origin = or.getText().toString();
String destination = dest.getText().toString();
if(Origin.isEmpty()){
Toast.makeText(this,"Please Enter the Origin" , Toast.LENGTH_SHORT).show();
}
if(destination.isEmpty()){
Toast.makeText(this,"Please Enter the Destination" , Toast.LENGTH_SHORT).show();
}
DirectionFinder directionFinder = new DirectionFinder(Origin, destination);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
/*double array [] = {44.968046 ,-94.420307 ,44.33328,-89.132008, 33.755787,-116.359998,33.844843,-116.54911 ,44.92057 ,-93.44786};
// Add a marker in Sydney and move the camera
for ( int i = 0 ; i< array.length ; i = i+2){
LatLng place = new LatLng( array[i], array[i+1]);
mMap.addMarker(new MarkerOptions().position(place).title("Marker in"+ i));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}
}
}
私のDirectionFinder.Javaは:
public DirectionFinder( String or , String dest) {
if(or.equals("PRAN RFL")){
double lat1 = 23.781388 ;
double lon1 = 90.425500 ;
LatLng Origin = new LatLng( lat1, lon1);
}
if(dest.equals("Gulshan")){
double lat2 = 23.780270 ;
double lon2 = 23.780270 ;
LatLng destination = new LatLng( lat2, lon2);
}
//this.listener = listener;
// this.Origin = Origin;
// this.destination = destination;
}
public void execute() throws UnsupportedEncodingException {
listener.onDirectionFinderStart();
new DownloadRawData().execute(createUrl());
}
private String createUrl() throws UnsupportedEncodingException {
String urlOrigin = URLEncoder.encode(Origin, "utf-8");
String urlDestination = URLEncoder.encode(destination, "utf-8");
return DIRECTION_URL_API + "Origin=" + urlOrigin + "&destination=" + urlDestination + "&key=" + GOOGLE_API_KEY;
}
private class DownloadRawData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String link = params[0];
try {
URL url = new URL(link);
InputStream is = url.openConnection().getInputStream();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String res) {
try {
parseJSon(res);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void parseJSon(String data) throws JSONException {
if (data == null)
return;
List<Route> routes = new ArrayList<Route>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
JSONObject jsonLeg = jsonLegs.getJSONObject(0);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
route.endAddress = jsonLeg.getString("end_address");
route.startAddress = jsonLeg.getString("start_address");
route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng"));
route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng"));
route.points = decodePolyLine(overview_polylineJson.getString("points"));
routes.add(route);
}
listener.onDirectionFinderSuccess(routes);
}
private List<LatLng> decodePolyLine(final String poly) {
int len = poly.length();
int index = 0;
List<LatLng> decoded = new ArrayList<LatLng>();
int lat = 0;
int lng = 0;
while (index < len) {
int b;
int shift = 0;
int result = 0;
do {
b = poly.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = poly.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
decoded.add(new LatLng(
lat / 100000d, lng / 100000d
));
}
return decoded;
}
}
私の活動のレイアウトは:
<LinearLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:map="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
tools:context="com.pran.trackingapp.MapsActivity"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<EditText
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:id="@+id/etOrigin"
Android:hint="Enter Origin address" />
<EditText
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:hint="Enter destination address"
Android:id="@+id/etDestination" />
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal"
>
<Button
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Find path"
Android:id="@+id/btnFindPath" />
<ImageView
Android:layout_marginLeft="20dp"
Android:layout_marginTop="5dp"
Android:layout_width="40dp"
Android:layout_height="40dp" />
<TextView
Android:layout_marginLeft="5dp"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="0 km"
Android:id="@+id/tvDistance" />
<ImageView
Android:layout_marginLeft="20dp"
Android:layout_marginTop="5dp"
Android:layout_width="40dp"
Android:layout_height="40dp"
Android:padding="5dp" />
<TextView
Android:layout_marginLeft="5dp"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="0 min"
Android:id="@+id/tvDuration" />
</LinearLayout>
<fragment
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="match_parent" />
</LinearLayout>
しかし、findpathボタンをクリックした後、エラーが発生しました:**
E/DynamiteModule:DynamiteLoaderの読み込みに失敗しました:Java.lang.ClassNotFoundException:パスにクラス "com.google.Android.gms.dynamite.DynamiteModule $ DynamiteLoaderClassLoader"が見つかりませんでした:DexPathList [[Zip file "/ data/app/com .pran.trackingapp-2/base.apk "]、nativeLibraryDirectories
**私のbuild.gradle(モジュール)は:
apply plugin: 'com.Android.application'
Android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.pran.trackingapp"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.Android.support:appcompat-v7:25.0.0'
compile 'com.google.Android.gms:play-services:9.8.0'
}
私のbuild.gradle(Project)は:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.Android.tools.build:gradle:2.0.0'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
とパスが描かれていません。どうすればよいですか?
アプリのgradleファイルに次の変更を加えます。
これはbuild.gradle(Project:// your project name)用です
追加 classpath 'com.google.gms:google-services:3.0.0'
依存関係
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.Android.tools.build:gradle:2.2.2'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
そして、これはあなたのbuild.gradle(Module:app)のためのものですGoogleが他のサービスを使用していない場合はこの行を追加しないでくださいmapsこれをファイルの上部または下部に追加するだけですapply plugin: 'com.google.gms.google-services'
これがお役に立てば幸いです。