0

Google マップ アプリで問題が発生しています。今のところ、アプリは自分の位置をズームインするためのサポートのみでしたが、機能していません。私の場所の離れた場所は、マップの上部で終わります。これが私のコードです: MainActivity:

public class MainActivity extends Activity implements LocationListener {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (isGooglePlayOk()) {

        setContentView(R.layout.activity_main);
        setMap();
        googleMap.setMyLocationEnabled(true);


    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.action_legalnotices:
        startActivity(new Intent(this, LegalNoticeActivity.class));
        return true;

    default:
        return false;
    }
}

private boolean isGooglePlayOk() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS) {
        return (true);
    }

    else {
        ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10))
                .show();

    }
    return (false);

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setMap() {

    if (googleMap == null) {

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.main.map)).getMap();
        if (googleMap != null) {

        }

        googleMap.setMyLocationEnabled(true);
        LocationManager la = (LocationManager) getSystemService(LOCATION_SERVICE);

        String provider = la.getBestProvider(new Criteria(), true);
        Location loc = la.getLastKnownLocation(provider);
        if (provider != null) {
            onLocationChanged(loc);

        }

        googleMap.setOnMapLongClickListener(onLongClickMapSettiins());

    }
}

private OnMapLongClickListener onLongClickMapSettiins() {
    // TODO Auto-generated method stub

    return new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng point) {
            Toast.makeText(getApplicationContext(), "OK",
                    Toast.LENGTH_SHORT).show();

        }
    };
}

@Override
public void onLocationChanged(Location location) {

    LatLng latlong = new LatLng(location.getLatitude(),
            location.getLongitude());

    googleMap.setMyLocationEnabled(true);

    CameraPosition cp = new CameraPosition.Builder().target(latlong)
            .zoom(15).build();

    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));

}

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

}

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

}

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

}
 }

アクティビティ_メイン:

    <?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+main/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

マニフェスト:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapateste"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="(My Api is working fine)" />

        <activity
            android:name="com.example.mapateste.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.mapateste.LegalNoticeActivity"
            android:label="@string/title_activity_legal_notice" >
        </activity>
    </application>

自分のデバイスでしかテストできません。マップは正常に機能していますが、mylocationbutton をクリックしただけではすぐに画面の中央に自分の位置を取得できません。誰かが私を助けることができますか?

4

8 に答える 8

1

最小 SDK は 8 です。使用する必要がありますSupportMapFragment。あなたのクラスは拡張する必要がありますFragmentActivtiy

以下のリンクの開発者ガイドの見出しの上の行を確認してください

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"  
android:layout_width="match_parent"
android:layout_height="match_parent"/>

使用するSupportMapFragment

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

サポート ライブラリが追加されていることを確認してください

また、以下をインポートしたことを確認してください

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment

ズームする

 CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latlong,20);
 googleMap.moveCamera(update);
于 2013-09-19T04:36:38.510 に答える
1

これは、Google マップ V2 のズーム機能を使用した現在地です。

 double lat= location.getLatitude();
 double lng = location.getLongitude();
 LatLng ll = new LatLng(lat, lng);
 googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 20));
于 2014-01-09T06:17:57.280 に答える
0

activity_map.xml は、setMap の条件が true の場合にマップを表示するためのものです。それ以外の場合は、通常の activity_main.xml に移動します。

public class MainActivity extends FragmentActivity {
    GoogleMap mMap;


if (isGooglePlayOk()) {

        setContentView(R.layout.activity_map);

        if (setMap()) {
            mMap.setMyLocationEnabled(true);
        }
        else {
            //your code
        }
    }
    else {
        setContentView(R.layout.activity_main);
    }

setMap のメソッド:

private boolean setMap() {
    if (mMap == null) {
        SupportMapFragment mapFrag =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mMap = mapFrag.getMap();
    }
    return (mMap != null);
}
于 2013-09-19T05:45:08.870 に答える
0

クラスは Fragment Activity を拡張する必要があります

public class MainActivity extends FragmentActivity {
//assign any arbitrary value to GPS_ERRODIALOG_REQUEST
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap googlemap;

}

Google Service Ok メソッドの場合、次のことを行います。

public boolean isGooglePlayOk(){
    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 Goolge Play", Toast.LENGTH_SHORT).show();
}
return false;

}

.xml ファイルでは、フラグメントを宣言するときに、最小 skd が 8 であるため、Map Fragment をサポートする必要があります。

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:maps="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
于 2013-09-19T05:24:09.507 に答える