我想在地图上显示当前位置的坐标。这是代码,它不会像它应该的那样工作。它只显示已定义参数中的地图,但是当我点击显示当前位置的按钮时,没有任何反应。 我在网上使用Android Studio教程制作了这段代码。 我正在使用Android工作室,只是一个新学习者。
这也是我在智能手机上运行应用程序时在事件日志中得到的结果:
21:56:48无法绑定到本地8600以获得调试器
21:56:49已建立的连接被主机中的软件中止了 java.io.IOException:已建立的连接已被主机中的软件中止 at sun.nio.ch.SocketDispatcher.write0(Native Method) at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51) 在sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) at sun.nio.ch.IOUtil.write(IOUtil.java:65) at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:487) 在com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213) 在com.android.ddmlib.Client.sendAndConsume(Client.java:686) 在com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349) 在com.android.ddmlib.Client.requestAllocationStatus(Client.java:525) 在com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:569) 在com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:544) 在com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:360) 在com.android.ddmlib.Devic ...
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity_koordinate"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
package com.example.apollo.kartamackovec;
import android.location.Location;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity_koordinate extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback, MapsActivity {
public GoogleMap mMap;
public GoogleApiClient client;
public TextView mLongitudeText;
public TextView mLatitudeText;
@Override
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);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng mackovec = new LatLng(46.4239, 16.4339);
mMap.addMarker(new MarkerOptions().position(mackovec).title("Marker u Mačkovcu"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mackovec));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mackovec, 18));
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
}
@Override
public void onConnected(Bundle connectionHint) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
Toast.makeText(this, "Location " + mLatitudeText+","+mLongitudeText,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "noconnection",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onStart() {
client.connect();
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.apollo.kartamackovec/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
client.disconnect();
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.apollo.kartamackovec/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "noconnection",
Toast.LENGTH_LONG).show();
}
}